使用事物之后锁表的问题 返回
使用IOC注入。
我有两个db分别为A,B,我首先打开Adb然后做数据处理之后,我又使用ChangeDatabase方法做B的切换,然后在做数据处理,之后我有将db切换为A,
最后又需要处理A的数据,但是在最后处理A数据的是出现死锁,找DBA抓取死锁日志如下图,请问是哪里的问题
//连接db方法
protected ISqlSugarClient ChangeDatabase(string? configId = null)
{
if (configId == null)
{
if (typeof(TEntity).GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).FirstOrDefault((x => x.GetType() == typeof(SugarTable))) is SugarTable sugarTable && !string.IsNullOrEmpty(sugarTable.TableDescription))
{
_dbBase.ChangeDatabase(sugarTable.TableDescription.ToLower());
}
else
{
_dbBase.ChangeDatabase(MainDb.CurrentDbConnId);
}
}
else
{
_dbBase.ChangeDatabase(configId.ToLower());
}
return _dbBase;
}
//1、处理A db数据
try
{
await Db.Ado.BeginTranAsync();
//A数据处理
await Db.Ado.CommitTranAsync();
}
catch (Exception ex)
{
await Db.Ado.RollbackTranAsync();
throw new Exception(ex.Message);
}
//2、
处理B db数据
var dbErp = ChangeDatabase(DBConfig.erpDB.ToString());
try
{
await dbErp.Ado.BeginTranAsync();
//B数据处理
await dbErp.Ado.CommitTranAsync();
}
catch (Exception ex)
{
await dbErp.Ado.RollbackTranAsync();
throw new Exception(ex.Message);
}
finally
{
ChangeDatabase();
}
//3、处理A db数据-----这一步其中死锁
try
{
await Db.Ado.BeginTranAsync();
//A处理数据
await Db.Ado.CommitTranAsync();
}
catch (Exception ex)
{
await Db.Ado.RollbackTranAsync();
throw new Exception(ex.Message);
}
//4、日志截图


热忱回答(3)
-
fate sta VIP0
2周前看文档:多租户 ( 里面有事务推荐。一般不推荐change用法设计要求过高,推荐用GetConnection)
0 回复 -
figther VIP0
2周前@fate sta:我看了多租户的用户,这样能保证B的DB发生问题之后统一回滚数据A和B的数据么,而且使用GetConnection还是GetConnectionScope
0 回复 -
figther VIP0
2周前@fate sta:而且我如果有A DB是主,那我是不是只用在做B DB操作的时候加上GetConnection,主DB是不是可以不需要
0 回复