特性事务中如何判断是真实提交或假提交? #1320
Unanswered
li-zheng-hao
asked this question in
Q&A
Replies: 3 comments 3 replies
-
_capPublisher 支持嵌套吗,否则应该适应不了 UnitOfWorkManager 传播的特点。 |
Beta Was this translation helpful? Give feedback.
1 reply
-
目前加了这句,感觉没啥问题 if (_uow.GetType().Name == "UnitOfWorkVirtual")
return;
|
Beta Was this translation helpful? Give feedback.
1 reply
-
看了一下CAP的源码,publisher是单例注册的: services.TryAddSingleton<ICapPublisher, CapPublisher>(); 内部的事务是单次请求内共享: public AsyncLocal<ICapTransaction> Transaction { get; } 所以我改了一下: public override void OnEntry(MethodContext context)
{
Log.Debug("进入 Transactional事务切面");
var uowManager = m_ServiceProvider!.Value!.GetService<UnitOfWorkManager>()!;
_uow = uowManager.Begin(this.Propagation, this.m_IsolationLevel);
var trans=_uow.GetOrBeginTransaction();
#region CAP相关
// 这里接入cap的事务 并且设置cap的自动提交为false
// ICapPublisher是单例的,里面的是事务是AsyncLocal存储
_capPublisher = m_ServiceProvider!.Value!.GetService<ICapPublisher>()!;
if (_capPublisher!.Transaction.Value is null)
{
_capTransaction = ActivatorUtilities.CreateInstance<MySqlCapTransaction>(m_ServiceProvider.Value);;
_capTransaction.AutoCommit = false;
_capTransaction.DbTransaction = trans;
_capPublisher.Transaction.Value = _capTransaction;
}
#endregion
Log.Debug($"当前事务的guid为{_uow.Orm.Ado.Identifier}");
// todo 获取一个mqtransaction并标记开启了事务
}
public override void OnExit(MethodContext context)
{
if (typeof(Task).IsAssignableFrom(context.RealReturnType))
((Task)context.ReturnValue).ContinueWith(t => _OnExit());
else _OnExit();
void _OnExit()
{
try
{
if (context.Exception == null)
{
Log.Error("UnitOfWorkManager 切面 提交 ");
_uow.Commit();
var uowName=_uow.GetType().Name;
// 假提交,CAP先不推消息
if ( uowName== "UnitOfWorkVirtual"||uowName=="UnitOfWorkNothing")
return;
_capTransaction?.GetType().GetMethod("Flush", BindingFlags.Instance | BindingFlags.NonPublic)?.Invoke(_capTransaction, null);
}
else
{
Log.Error("UnitOfWorkManager 切面 回滚 ");
_uow.Rollback();
}
}
finally
{
_uow.Dispose();
}
}
Log.Debug("退出 Transactional事务切面");
} 简单测试了一下,感觉没啥问题了 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
之前集成了CAP和Freesql的特性事务,AOP是这样写的:
最近发现上面的代码有点问题:
如果_uow这里是假提交的话,下面cap也不应该提交,但是我没找到哪里可以判断是否假提交,请问有什么方法可以解决这个问题吗?
Beta Was this translation helpful? Give feedback.
All reactions