public class LocationController : Controller
{
MySqlConnection myConnnect;
MySqlCommand sqlCmd;
public LocationController()
{
string constructorString = "server=127.0.0.1;User Id=root;password=123456;Database=test;";
myConnnect = new MySqlConnection(constructorString);
sqlCmd = new MySqlCommand();
sqlCmd.Connection = myConnnect;
sqlCmd.CommandText = "select * from location where parent_id=0";
}
public async Task<List<Dictionary<string, string>>> Index()
{
var list = new List<Dictionary<string, string>>();
await myConnnect.OpenAsync();
using (var reader = await sqlCmd.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
var data = new Dictionary<string, string>();
data["id"] = reader.GetInt32(0).ToString();
data["spell_first"] = reader.GetString(2);
data["spell_full"] = reader.GetString(3);
data["name"] = reader.GetString(4);
list.Add(data);
}
}
await myConnnect.CloseAsync();
return list;
}
}
ab.exe -c 1000 -n 50000 http://192.168.2.104:5000/location
ubuntu20.04
压测的时候。这段代码 dotnet进程cpu很高 压测的时候出现以下错误(没出错的时候qps 才600):
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
---> MySql.Data.MySqlClient.MySqlException (0x80004005): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
at MySql.Data.Common.StreamCreator.GetTcpStream(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at System.Data.Common.DbConnection.OpenAsync(CancellationToken cancellationToken)
--- End of stack trace from previous location ---
at site.Controllers.LocationController.Index() in D:\project\hefei\site\site\Controllers\LocationController.cs:line 30
at lambda_method4(Closure , Object )
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
很久没用c#了。我打开方式不对?
错误提示已经说明了情况:数据库连接池不可以再创建新的连接了 at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
。
考虑一下怎么合理的使用 DbConnection
对象
或者
根据访问量更改一下 Max Pool Size
https://docs.microsoft.com/zh-cn/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-5.0
试过。还是一样出错。
thread.setminthreads() 改一下线程池最大和最小数量.
你这样写 我理解是 只有一个myConnnect
mysqlconnection默认是使用连接池的。我把它放到Index里面每次都创建新的一样的结果。