dotnetcore / dotnetcore/FreeSql

MySQL UnitOfWork事务提交时出错再次使用此连接会导致事务嵌套

Open
#2,003 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.4k
Forks
910
PR merge metrics
No merged PRs in 30d

Description

使用MySqlConnector驱动
创建UnitOfWork事务
在提交时将数据库重启,再次使用此连接会提示 Transactions may not be nested.

建议 UnitOfWork 还回对象时 若事务不为空 将对象释放
```c#
// 原代码:
void ReturnObject()
{
if (string.IsNullOrEmpty(this.Id) == false && DebugBeingUsed.TryRemove(this.Id, out var old))
this.Id = null;

_fsql.Ado.MasterPool.Return(_conn);
_tran = null;
_conn = null;
EntityChangeReport?.Report.Clear();
}
// 调整后:
void ReturnObject()
{
if (string.IsNullOrEmpty(this.Id) == false && DebugBeingUsed.TryRemove(this.Id, out var old))
this.Id = null;

_fsql.Ado.MasterPool.Return(_conn);
_tran?.Dispose();
_tran = null;
_conn = null;
EntityChangeReport?.Report.Clear();
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.