Skip to content

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

Open
@dannyxzh

Description

@dannyxzh

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

建议 UnitOfWork 还回对象时 若事务不为空 将对象释放

  //  原代码:
      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();
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions