在 dapper 中进行连接管理的最佳方式?
Best way to do connection management in dapper?
我在 MySQL
数据库上使用以下方式在 dapper 中查询。
using (var db = new MySqlConnection(ConfigurationHandler.GetSection<string>(StringConstants.ConnectionString)))
{
resultSet = db.Execute(UpdateQuery, new { _val = terminalId }, commandType: CommandType.Text);
db.Close();//should i call this or not
db.Dispose();//should i call this or not
}
显式调用 db.close 和 db.dispose 是一种好方法吗?我的应用程序每秒可以处理 100 个请求。
using 块是围绕 IDisposable
界面的一种便利。它确保在块的末尾调用 dispose 方法。
参见:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement。
在您的情况下,您可以删除对 db.Close() 和 db.Dispose() 的显式调用,因为您没有重新使用连接对象。
using (var db = new MySqlConnection(ConfigurationHandler.GetSection<string>(StringConstants.ConnectionString)))
{
resultSet = db.Execute(UpdateQuery,
new { _val = terminalId }, commandType: CommandType.Text);
}
以下 link 提供了有关 .Close 与 .Dispose 的更多详细信息:
我在 MySQL
数据库上使用以下方式在 dapper 中查询。
using (var db = new MySqlConnection(ConfigurationHandler.GetSection<string>(StringConstants.ConnectionString)))
{
resultSet = db.Execute(UpdateQuery, new { _val = terminalId }, commandType: CommandType.Text);
db.Close();//should i call this or not
db.Dispose();//should i call this or not
}
显式调用 db.close 和 db.dispose 是一种好方法吗?我的应用程序每秒可以处理 100 个请求。
using 块是围绕 IDisposable
界面的一种便利。它确保在块的末尾调用 dispose 方法。
参见:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement。
在您的情况下,您可以删除对 db.Close() 和 db.Dispose() 的显式调用,因为您没有重新使用连接对象。
using (var db = new MySqlConnection(ConfigurationHandler.GetSection<string>(StringConstants.ConnectionString)))
{
resultSet = db.Execute(UpdateQuery,
new { _val = terminalId }, commandType: CommandType.Text);
}
以下 link 提供了有关 .Close 与 .Dispose 的更多详细信息: