ow
This commit is contained in:
commit
f05d2d215f
678 files changed
+73865
No files matched your search
@@ -0,0 +1,104 @@
|
||||
using CloudBuilder.Core.Service;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Transactions;
|
||||
|
||||
namespace CloudBuilder.Core.DatabaseAccessor
|
||||
{
|
||||
/*
|
||||
事务范围:TransactionScopeOption.RequiresNew 管理的是分布式事务,
|
||||
TransactionScopeOption.Required但对于单个数据库连接,通常不需要分布式事务。
|
||||
OnActionExecutionAsync 是 ASP.NET Core 中异步的 Action 过滤器执行方法。
|
||||
// OnActionExecuting: Action 执行前
|
||||
[开始事务]
|
||||
↓
|
||||
[Action 方法执行]
|
||||
↓
|
||||
// OnActionExecuted: Action 执行后(无论是否异常)
|
||||
[检查异常]
|
||||
//Controller对象不抛出异常,返回的是异常对象ValidatedException、ServiceErrorException(方便前端信息统一处理)
|
||||
[提交或回滚事务]
|
||||
*/
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class UnitOfWorkAttribute : ActionFilterAttribute
|
||||
{
|
||||
private TransactionScope _transactionScope;
|
||||
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
_transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew,
|
||||
new TransactionOptions
|
||||
{
|
||||
IsolationLevel = IsolationLevel.ReadCommitted,
|
||||
Timeout = TransactionManager.MaximumTimeout
|
||||
});
|
||||
|
||||
base.OnActionExecuting(context);
|
||||
}
|
||||
|
||||
//public override void OnActionExecuting(ActionExecutingContext context)
|
||||
//{
|
||||
// // 正确:单机数据库事务(不是分布式)
|
||||
// _transactionScope = new TransactionScope(
|
||||
// TransactionScopeOption.Required,
|
||||
// new TransactionOptions
|
||||
// {
|
||||
// IsolationLevel = IsolationLevel.ReadCommitted
|
||||
// },
|
||||
// TransactionScopeAsyncFlowOption.Enabled); // 必须加这个
|
||||
|
||||
// base.OnActionExecuting(context);
|
||||
//}
|
||||
|
||||
public override void OnActionExecuted(ActionExecutedContext context)
|
||||
{
|
||||
if (_transactionScope == null)
|
||||
{
|
||||
base.OnActionExecuted(context);
|
||||
return;
|
||||
}
|
||||
|
||||
bool needRollback = false;
|
||||
|
||||
try
|
||||
{
|
||||
// ==============================================
|
||||
// 核心逻辑:判断返回值是不是异常对象
|
||||
// ==============================================
|
||||
if (context.Result is ObjectResult objectResult)
|
||||
{
|
||||
var value = objectResult.Value;
|
||||
|
||||
if (value != null)
|
||||
{
|
||||
// 只要是这两个异常 → 必须回滚
|
||||
if (value is ValidatedException || value is ServiceErrorException)
|
||||
{
|
||||
needRollback = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有异常,才提交
|
||||
if (!needRollback)
|
||||
{
|
||||
//必须要有 SaveChanges() 调用,否则数据只会停留在内存中,不会真正保存到数据库。
|
||||
_transactionScope.Complete();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
// 不 Complete() 就是自动回滚
|
||||
_transactionScope.Dispose();
|
||||
}
|
||||
|
||||
base.OnActionExecuted(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user