Commit d258b479 authored by 潘栩锋's avatar 潘栩锋 🚴

修复 在SQLite 使用 using (SQLiteTransaction tran = connection.BeginTransaction()),解决tran释放问题

parent 6cb82319
...@@ -491,9 +491,9 @@ namespace SQLite ...@@ -491,9 +491,9 @@ namespace SQLite
/// <returns></returns> /// <returns></returns>
public bool QueryTran(IEnumerable<string> queryList) public bool QueryTran(IEnumerable<string> queryList)
{ {
if (isHoldQueryTran) if (isHoldQueryTran)//打包模式
{ {
holdQueryList.AddRange(queryList); holdQueryList.AddRange(queryList);//记录SQLs
return true; return true;
} }
else else
...@@ -504,7 +504,8 @@ namespace SQLite ...@@ -504,7 +504,8 @@ namespace SQLite
{ {
connection.Open(); connection.Open();
SQLiteTransaction tran = connection.BeginTransaction(); using (SQLiteTransaction tran = connection.BeginTransaction())
{
bool check = false; bool check = false;
try try
{ {
...@@ -513,24 +514,22 @@ namespace SQLite ...@@ -513,24 +514,22 @@ namespace SQLite
command.CommandText = item; command.CommandText = item;
command.ExecuteNonQuery(); command.ExecuteNonQuery();
} }
tran.Commit(); tran.Commit();//事务提交
check = true; check = true;//执行成功
} }
catch (Exception ex) catch (Exception ex)
{ {
tran.Rollback(); //异常
check = false; tran.Rollback();//事务回滚
check = false;//执行失败
logger.Error(ex, Newtonsoft.Json.JsonConvert.SerializeObject(queryList)); logger.Error(ex, Newtonsoft.Json.JsonConvert.SerializeObject(queryList));
throw ex; throw ex;//抛出异常
}
finally
{
connection.Close();
} }
return check; return check;
} }
} }
} }
}
} }
...@@ -564,35 +563,36 @@ namespace SQLite ...@@ -564,35 +563,36 @@ namespace SQLite
{ {
connection.Open(); connection.Open();
SQLiteTransaction tran = connection.BeginTransaction(); using (SQLiteTransaction tran = connection.BeginTransaction())
bool check = false; {
bool check;
try try
{ {
foreach (string item in holdQueryList) foreach (string item in holdQueryList)//全部SQL 打包为一个事务执行
{ {
command.CommandText = item; command.CommandText = item;
command.ExecuteNonQuery(); command.ExecuteNonQuery();
} }
tran.Commit(); tran.Commit();//事务提交
check = true; check = true;//执行成功
} }
catch (Exception ex) catch (Exception ex)
{ {
tran.Rollback(); tran.Rollback();//异常出错,事务回滚
check = false; check = false;//执行失败
throw ex; throw ex;//弹出异常
} }
finally finally
{ {
holdQueryList.Clear(); holdQueryList.Clear();//清除 SQL
connection.Close(); isHoldQueryTran = false;//退出打包模式
isHoldQueryTran = false;
} }
return check; return check;
} }
} }
} }
isHoldQueryTran = false; }
isHoldQueryTran = false;//退出打包模式
return true; return true;
} }
/// <summary> /// <summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment