一个应用程序在从连接池获取连接之前给出了经过的异常超时时间。我使用了下面的代码,这个代码片段是从不同的并发用户调用的,最大命中率高达10000 pr.second。我已经使用Dapper从Azure MS SQL数据库中获取结果。
public async Task<List<Results>> GetDBResults(string Id, int skip, int take)
{
var values = new DynamicParameters();
values.Add("Id", Id);
values.Add("Skip", skip);
values.Add("Take", take);
using var connection = GetConnection(AppSettingsProvider.TryGet("ConnectionString"));
try
{
var sw = Stopwatch.StartNew();
//connection.Open();
// QueryAsync is from Dapper
var dbResult = await connection.QueryAsync<ResponseObject>("SP_name", param: values,
commandType: CommandType.StoredProcedure, commandTimeout: Constants.CommandTimeout);
var result= dbResult?.ToList();
Console.WriteLine("execution time = {0} seconds\n", sw.Elapsed.TotalSeconds);
return result;
}
finally
{
connection.Close();
}
}
private SqlConnection GetConnection(string connectionString)
{
var sqlConnection = new SqlConnection(connectionString);
return sqlConnection;
}
我知道,'using‘将关闭并释放connection对象,连接正在关闭,但DB池不能立即用于下一个DB连接。因此,我在最后一块中显式地关闭了DB连接。这使得我能够成功地执行更多的并发请求。在那之后,我得到了超时异常。
Connection.Open由Dapper管理,因此不会向代码片段中添加任何connection.Open。
在并发用户超过200次点击后,我们得到了超时问题。
请告诉我这个问题的解决方案。
发布于 2021-09-28 07:43:21
这似乎是一个池耗尽问题;如果您每秒有10000个请求,并注意到default connection pool size is 100,那么即使其他一切都运行得很好(从不CPU耗尽、GC停止等),那么您的“获取、执行、释放”循环只有10ms的时间(因为100个连接中的每个连接每秒都需要处理100个请求才能达到10k/s的请求总数)。老实说,这是相当雄心勃勃的,即使是在有良好局域网的硬件上-如果你在云中("Azure MS SQL"):算了吧(除非你正在为非常非常高的服务付费)。你可以试着增加池子的大小,但老实说,我想知道这里是不是应该重新设计一下。您以秒为单位测量时间的事实强调了这里问题的规模:
Console.WriteLine("execution time = {0} seconds\n", sw.Elapsed.TotalSeconds);
https://stackoverflow.com/questions/69302074
复制相似问题