[英]MySqlConnection ADO.Net High CPU Usage
我有一个多线程应用程序,其中N个线程正在访问MySQL数据库。 每个查询将创建自己的MySqlConnection实例,以避免共享MySqlDataReader 。 但是,我们遇到数据库服务器的CPU高峰。 我已经尝试运行SHOW PROCESSLIST,但是结果中没有慢查询。 我怀疑根本原因是线程如何处理连接。
以下是我的查询方法示例。
public List<Configuration> GetAlertConfigByDeviceID(string deviceID)
{
List<Configuration> configurations = new List<Configuration>();
try
{
using (MySqlConnection dbConnection = new MySqlConnection(WinAlerterConfig.WebConnectionString))
{
dbConnection.Open();
using (MySqlCommand dbCommand = dbConnection.CreateCommand())
{
dbCommand.CommandText = StoredProcedures.SqlGetAlertCfgDetailByDeviceID;
dbCommand.Parameters.Add(new MySqlParameter("DeviceID", deviceID));
using (MySqlDataReader dbResultSet = dbCommand.ExecuteReader())
{
if (dbResultSet.HasRows)
{
while (dbResultSet.Read())
{
configurations.Add(new Configuration()
{
ConfigID = dbResultSet.GetUInt64Safe(0),
AlertType = dbResultSet.GetUInt64Safe(1),
ZoneID = dbResultSet.GetUInt64Safe(2),
Timeout = dbResultSet.GetInt64Safe(3),
DeviceID = dbResultSet.GetStringSafe(4),
FromZoneID = dbResultSet.GetUInt64Safe(5),
OtherZoneAlert = dbResultSet.GetBooleanSafe(6),
Remarks = dbResultSet.GetStringSafe(7)
});
}
}
dbResultSet.Close();
dbResultSet.Dispose();
}
dbCommand.Dispose();
}
dbConnection.Close();
dbConnection.Dispose();
}
}
catch (MySqlException ex)
{
Logger.LogErrorLine("GetAlertConfigByDeviceID: {0} {1} {2}", ex.Message, Environment.NewLine, ex.StackTrace);
}
catch (Exception ex)
{
Logger.LogErrorLine("GetAlertConfigByDeviceID: {0} {1} {2}", ex.Message, Environment.NewLine, ex.StackTrace);
}
return configurations;
}
我还添加了“ polling = true;” 在我们的连接字符串中。 因此,我假设MySQL或ADO.net将处理连接池。
我的方法做错了吗? 连接池是否已经由ADO.net处理?
删除所有关闭和处置呼叫。 使用块已经在处理中。
还要确保在deviceId列上有一个索引。
然后再试一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.