sql – 使用Dapper时关闭连接
一旦查询执行显式调用Close方法或将连接放在Using语句中,是否有必要关闭连接?离开连接是否会导致连接重用并提高SQL性能以备将来查询?
解决方法我假设您使用的是最新版本的Dapper.使用Dapper,有两种方法可以管理连接: >全面管理自己:
当然,您可以在单个连接上调用多个查询.但是,应该关闭连接(通过调用Close(),Dispose()方法或将其封闭在使用块中)以避免资源泄漏.关闭连接会将其返回到连接池.连接池的参与提高了新连接成本的性能. 除了处理连接之外,我建议你实现UnitOfWork来管理事务.在GitHub上参考this优秀样本. 以下源代码可以帮助您.请注意,这是为了我的需要而写的;所以它可能不适合你. public sealed class DalSession : IDisposable { public DalSession() { _connection = new OleDbConnection(DalCommon.ConnectionString); _connection.Open(); _unitOfWork = new UnitOfWork(_connection); } IDbConnection _connection = null; UnitOfWork _unitOfWork = null; public UnitOfWork UnitOfWork { get { return _unitOfWork; } } public void Dispose() { _unitOfWork.Dispose(); _connection.Dispose(); } } public sealed class UnitOfWork : IUnitOfWork { internal UnitOfWork(IDbConnection connection) { _id = Guid.NewGuid(); _connection = connection; } IDbConnection _connection = null; IDbTransaction _transaction = null; Guid _id = Guid.Empty; IDbConnection IUnitOfWork.Connection { get { return _connection; } } IDbTransaction IUnitOfWork.Transaction { get { return _transaction; } } Guid IUnitOfWork.Id { get { return _id; } } public void Begin() { _transaction = _connection.BeginTransaction(); } public void Commit() { _transaction.Commit(); Dispose(); } public void Rollback() { _transaction.Rollback(); Dispose(); } public void Dispose() { if(_transaction != null) _transaction.Dispose(); _transaction = null; } } interface IUnitOfWork : IDisposable { Guid Id { get; } IDbConnection Connection { get; } IDbTransaction Transaction { get; } void Begin(); void Commit(); void Rollback(); } 现在,您的存储库应该以某种方式接受此UnitOfWork.我选择了使用Constructor的依赖注入. public sealed class MyRepository { public MyRepository(IUnitOfWork unitOfWork) { this.unitOfWork = unitOfWork; } IUnitOfWork unitOfWork = null; //You also need to handle other parameters like 'sql','param' ect. This is out of scope of this answer. public MyPoco Get() { return unitOfWork.Connection.Query(sql,param,unitOfWork.Transaction,.......); } public void Insert(MyPoco poco) { return unitOfWork.Connection.Execute(sql,.........); } } 然后你这样称呼它: 随着交易: using(DalSession dalSession = new DalSession()) { UnitOfWork unitOfWork = dalSession.UnitOfWork; unitOfWork.Begin(); try { //Your database code here MyRepository myRepository = new MyRepository(unitOfWork); myRepository.Insert(myPoco); //You may create other repositories in similar way in same scope of UoW. unitOfWork.Commit(); } catch { unitOfWork.Rollback(); throw; } } 没有交易: using(DalSession dalSession = new DalSession()) { //Your database code here MyRepository myRepository = new MyRepository(dalSession.UnitOfWork);//UoW have no effect here as Begin() is not called. myRepository.Insert(myPoco); } 这样,您可以在一个位置控制它,而不是直接在您的调用代码中公开连接. 有关上述代码中存储库的更多详细信息,请参见here. 请注意,UnitOfWork是more,而不仅仅是一项交易.此代码仅处理事务.您可以扩展此代码以涵盖其他角色. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |