|
|
@@ -152,18 +152,82 @@ namespace ServiceCenter.SqlSugars
|
|
|
public static void Do(Action<SqlSugarHelper> act)
|
|
|
{
|
|
|
if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
|
|
|
+
|
|
|
+ // 创建独立的SqlSugarHelper实例,避免共享事务状态
|
|
|
var db = new SqlSugarHelper();
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ // 使用 SqlSugar 的 Ado.UseTran 方法进行事务管理(推荐方式)
|
|
|
+ var result = db.Connect.Ado.UseTran(() =>
|
|
|
+ {
|
|
|
+ act(db);
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!result.IsSuccess)
|
|
|
+ {
|
|
|
+ if (result.ErrorMessage.Contains("SqlTransaction"))
|
|
|
+ {
|
|
|
+ throw new Exception($"事务处理失败 - {result.ErrorMessage}。这通常是由于事务状态冲突导致的,请检查并发访问情况。");
|
|
|
+ }
|
|
|
+ throw new Exception($"事务执行失败: {result.ErrorMessage}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ // 如果 UseTran 方法不可用,回退到手动事务管理
|
|
|
+ if (ex.Message.Contains("UseTran") || ex is System.MissingMethodException)
|
|
|
+ {
|
|
|
+ DoWithManualTransaction(act, db);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 手动事务管理的回退方法
|
|
|
+ /// </summary>
|
|
|
+ private static void DoWithManualTransaction(Action<SqlSugarHelper> act, SqlSugarHelper db)
|
|
|
+ {
|
|
|
+ var transactionStarted = false;
|
|
|
try
|
|
|
{
|
|
|
- db.Connect.BeginTran();//开始事务
|
|
|
- act(db);//执行委托
|
|
|
- db.Connect.CommitTran();//提交事务
|
|
|
+ // 检查是否已经在事务中
|
|
|
+ if (db.Connect.Ado.Transaction == null)
|
|
|
+ {
|
|
|
+ db.Connect.BeginTran();
|
|
|
+ transactionStarted = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ act(db);
|
|
|
+
|
|
|
+ if (transactionStarted)
|
|
|
+ {
|
|
|
+ db.Connect.CommitTran();
|
|
|
+ }
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- db.Connect.RollbackTran();//回滚事务
|
|
|
- if (ex.Message.Contains("SqlTransaction")) throw new Exception($"{ex.Message}:{ex.StackTrace}");
|
|
|
- throw new Exception(ex.Message);
|
|
|
+ if (transactionStarted)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ db.Connect.RollbackTran();
|
|
|
+ }
|
|
|
+ catch (Exception rollbackEx)
|
|
|
+ {
|
|
|
+ throw new Exception($"原始错误: {ex.Message}; 回滚失败: {rollbackEx.Message}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ex.Message.Contains("SqlTransaction"))
|
|
|
+ {
|
|
|
+ throw new Exception($"事务处理失败 - {ex.Message}。这通常是由于事务已被提交或回滚后再次使用导致的。建议检查事务的使用方式和并发访问。");
|
|
|
+ }
|
|
|
+ throw new Exception($"事务执行失败: {ex.Message}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -177,20 +241,88 @@ namespace ServiceCenter.SqlSugars
|
|
|
public static T Do<T>(Func<SqlSugarHelper, T> act)
|
|
|
{
|
|
|
if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
|
|
|
+
|
|
|
+ // 创建独立的SqlSugarHelper实例,避免共享事务状态
|
|
|
var db = new SqlSugarHelper();
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ // 使用 SqlSugar 的 Ado.UseTran 方法进行事务管理(推荐方式)
|
|
|
+ var result = db.Connect.Ado.UseTran(() =>
|
|
|
+ {
|
|
|
+ db.Connect.Ado.CommandTimeOut = 10;
|
|
|
+ return act(db);
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!result.IsSuccess)
|
|
|
+ {
|
|
|
+ if (result.ErrorMessage.Contains("SqlTransaction"))
|
|
|
+ {
|
|
|
+ throw new Exception($"事务处理失败 - {result.ErrorMessage}。这通常是由于事务状态冲突导致的,请检查并发访问情况。");
|
|
|
+ }
|
|
|
+ throw new Exception($"事务执行失败: {result.ErrorMessage}");
|
|
|
+ }
|
|
|
+
|
|
|
+ return result.Data;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ // 如果 UseTran 方法不可用,回退到手动事务管理
|
|
|
+ if (ex.Message.Contains("UseTran") || ex is System.MissingMethodException)
|
|
|
+ {
|
|
|
+ return DoWithManualTransaction(act, db);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 手动事务管理的回退方法(带返回值)
|
|
|
+ /// </summary>
|
|
|
+ private static T DoWithManualTransaction<T>(Func<SqlSugarHelper, T> act, SqlSugarHelper db)
|
|
|
+ {
|
|
|
+ var transactionStarted = false;
|
|
|
try
|
|
|
{
|
|
|
- db.Connect.BeginTran();//开始事务
|
|
|
+ // 检查是否已经在事务中
|
|
|
+ if (db.Connect.Ado.Transaction == null)
|
|
|
+ {
|
|
|
+ db.Connect.BeginTran();
|
|
|
+ transactionStarted = true;
|
|
|
+ }
|
|
|
+
|
|
|
db.Connect.Ado.CommandTimeOut = 10;
|
|
|
- var res = act(db);//执行委托
|
|
|
- db.Connect.CommitTran();//提交事务
|
|
|
- return res;
|
|
|
+ var result = act(db);
|
|
|
+
|
|
|
+ if (transactionStarted)
|
|
|
+ {
|
|
|
+ db.Connect.CommitTran();
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- db.Connect.RollbackTran();//回滚事务
|
|
|
- if (ex.Message.Contains("SqlTransaction")) throw new Exception($"{ex.Message}:{ex.StackTrace}");
|
|
|
- throw new Exception(ex.Message);
|
|
|
+ if (transactionStarted)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ db.Connect.RollbackTran();
|
|
|
+ }
|
|
|
+ catch (Exception rollbackEx)
|
|
|
+ {
|
|
|
+ throw new Exception($"原始错误: {ex.Message}; 回滚失败: {rollbackEx.Message}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ex.Message.Contains("SqlTransaction"))
|
|
|
+ {
|
|
|
+ throw new Exception($"事务处理失败 - {ex.Message}。这通常是由于事务已被提交或回滚后再次使用导致的。建议检查事务的使用方式和并发访问。");
|
|
|
+ }
|
|
|
+ throw new Exception($"事务执行失败: {ex.Message}");
|
|
|
}
|
|
|
}
|
|
|
}
|