开发框架修正重要bug,使用SqlDataAdapter.Update方法后没有关闭SQL连接
开发框架修正重要bug,使用SqlDataAdapter.Update方法后没有关闭SQL连接
解决方案:
扫一扫加作者微信
解决方案:
定义_CurrentConnection 成员变量,BeginTransaction方法内实例化,然后在CommitTransaction,RollbackTransaction方法内关闭连接。
若没有使用dalBase基类,定制的源码使用SqlDataAdapter.Update方法要注意关闭连接。
修改dalBase.cs文件,新加及修改的代码参考:
C# Code:
/// <summary>
/// 当前启用的事务
/// </summary>
protected SqlTransaction _CurrentTrans = null;
/// <summary>
/// 当前事务的连接
/// </summary>
protected SqlConnection _CurrentConnection = null;
/// <summary>
/// 启用事务控制
/// </summary>
protected virtual void BeginTransaction()
{
_CurrentConnection = DataProvider.Instance.CreateConnection(_DBName);
_CurrentTrans = _CurrentConnection.BeginTransaction();
}
/// <summary>
/// 提交事务
/// </summary>
protected virtual void CommitTransaction()
{
try
{
if (_CurrentTrans != null)
{
_CurrentTrans.Commit();
_CurrentTrans = null;
DataProvider.Instance.CloseConnection(_CurrentConnection);
}
}
catch
{
DataProvider.Instance.CloseConnection(_CurrentConnection);
throw;
}
}
/// <summary>
/// 事务回滚
/// </summary>
protected virtual void RollbackTransaction()
{
try
{
if (_CurrentTrans != null)
{
_CurrentTrans.Rollback();
_CurrentTrans = null;
DataProvider.Instance.CloseConnection(_CurrentConnection);
}
}
catch
{
DataProvider.Instance.CloseConnection(_CurrentConnection);
throw;
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
/// <summary>
/// 当前启用的事务
/// </summary>
protected SqlTransaction _CurrentTrans = null;
/// <summary>
/// 当前事务的连接
/// </summary>
protected SqlConnection _CurrentConnection = null;
/// <summary>
/// 启用事务控制
/// </summary>
protected virtual void BeginTransaction()
{
_CurrentConnection = DataProvider.Instance.CreateConnection(_DBName);
_CurrentTrans = _CurrentConnection.BeginTransaction();
}
/// <summary>
/// 提交事务
/// </summary>
protected virtual void CommitTransaction()
{
try
{
if (_CurrentTrans != null)
{
_CurrentTrans.Commit();
_CurrentTrans = null;
DataProvider.Instance.CloseConnection(_CurrentConnection);
}
}
catch
{
DataProvider.Instance.CloseConnection(_CurrentConnection);
throw;
}
}
/// <summary>
/// 事务回滚
/// </summary>
protected virtual void RollbackTransaction()
{
try
{
if (_CurrentTrans != null)
{
_CurrentTrans.Rollback();
_CurrentTrans = null;
DataProvider.Instance.CloseConnection(_CurrentConnection);
}
}
catch
{
DataProvider.Instance.CloseConnection(_CurrentConnection);
throw;
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
扫一扫加作者微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网