我最近收到了一个mysql到很多连接错误,我已经使用了sql查询,如下面的那个
SET GLOBAL max_conmnections = 8000;
并且我也提高了mysql.pool.max to 8000
,当我的模拟器在调试器中时,它在此虚空中崩溃
private static SqlDatabaseClient CreateClient(int Id)
{
MySqlConnection Connection = new MySqlConnection(GenerateConnectionString());
Connection.Open();
return new SqlDatabaseClient(Id, Connection);
}
突出显示导致崩溃的行是connection.open();
当我收到10-12个在线连接时,模拟器在调试器中运行了7-8个小时!
答案 0 :(得分:1)
您可以尝试在C#using语句使用后关闭并处置连接和命令:
private static SqlDatabaseClient CreateClient(int Id)
{
Int32 returnId = 0;
try
{
using(MySqlConnection connection = new MySqlConnection(GenerateConnectionString()))
{
connection.Open();
if(connection.State == ConnectionState.Open)
{
returnId = Id;
}
}
}
catch(Exception exception)
{
Console.Write(ex.Message);
}
finally
{
if(connection.State == ConnectionState.Open)
{
connection.Close();
}
}
return returnId;
}
答案 1 :(得分:0)
我建议改写成:
private static void ExecuteInClientContext(int Id, Action<SqlDatabaseClient> callback) {
if (callback == null) {
throw new ArgumentNullException("callback");
}
using(MySqlConnection Connection = new MySqlConnection(GenerateConnectionString())) {
Connection.Open();
callback.Invoke(new SqlDatabaseClient(Id, Connection));
}
}
static void Foo() {
ExecuteInClientContext(1, (context) => {
// whatever
});
}
答案 2 :(得分:0)
我认为您可以添加以下代码:
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
然后,在关闭SqlDataReader的实例后,Connection对象也将被关闭。