The transaction associated with this command is not the connection‘s active

简介: The transaction associated with this command is not the connection‘s active

1、出现问题原因

接口接收入参为数组类型,遍历数组操作时,事务写在了循环里,待下一次循环时事务非此连接的事务导致出现如题错误,接口传参数组形式如下:

[
    {
        "id": 1,
        "infos": [
            {
                "id": "1",
                "name": "小华",
                "age": "18"
            }
        ]
    },
    {
        "id": 2,
        "infos": []
    }
]

image.gif

2、解决办法

将循环中需要更新的数据放入到列表中,在循环外用事务执行批量更新,以此使事务处于连接的活动事务中即可。部分示例代码如下:

// ...处理循环信息
var trans = _people.BeginTransaction();
try
{
    // 更新人员信息
    var updatePeople = _people.UpdateRange(updatePeopleInfoList);
    if(updatePeopleInfoList.Count == 0) updatePeople = true;
    // 更新人员附属信息
    var updatePeopleItem = _peopleItem.UpdateRange(updatePeopleItemList);
    if(updatePeopleItemList.Count == 0) updatePeopleItem = true;
    if(updatePeople && updatePeopleItem)
    {
        trans.Commit();
        _logger.LogDebug($ "[更新人员信息成功]", "111111");
    }
    else
    {
        trans.Rollback();
        _logger.LogError($ "[更新人员信息失败]{args.ToJson()}");
        return ErrorResult < long > ($ "更新人员信息失败", "111112");
    }
}
catch(Exception ex)
{
    trans.Rollback();
    _logger.LogError($ "[更新人员信息失败]{ex}", ex.Message);
    return ErrorResult < long > ("系统繁忙,请重新获取", "111150");
}

image.gif

——————————————————————————————————————————

2023-4-18 更新

上述代码采用IDbTransaction事务,可能是IDbTransaction封装有问题导致,重新排查代码,选择直接用自己封装的事务工具类,核心代码如下,自封装工具代码仓库代码量多不在此贴出来,如有需要欢迎评论区或者私信交流。

_people.BeginTransaction();
 try
 {
     var insertPeople = _people.Insert(peopleInfo);
     if(insertPeople)
     {
         _people.CommitTransaction();
     }
     else
     {
         _people.RollBackTransaction();
     }
 }
 catch(Exception ex)
 {
     _people.RollBackTransaction();
 }

image.gif

若本文有帮助到阅读本文的同学,欢迎点赞、关注、收藏,互相学习交流。

目录
相关文章
|
Web App开发 前端开发
【前端异常】Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
【前端异常】Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
863 0
|
7月前
|
SQL 关系型数据库 MySQL
SQL Error (2013): Lost connection to MySQL server at 'waiting for initial communication packet', sys...
SQL Error (2013): Lost connection to MySQL server at 'waiting for initial communication packet', sys...
196 0
【已解决】kex_exchange_identification: Connection closed by remote host fatal: Could not read from
【已解决】kex_exchange_identification: Connection closed by remote host fatal: Could not read from
|
7月前
|
负载均衡 Java 应用服务中间件
Client not connected, current status:STARTING
Client not connected, current status:STARTING
581 1
|
Java 关系型数据库 MySQL
定时任务Quzrtz:Failed to override connection auto commit/transaction isolation
定时任务Quzrtz:Failed to override connection auto commit/transaction isolation
155 0
|
数据库
Could not create connection to database server. Attempted reconnect 3 times. Giving up.
Could not create connection to database server. Attempted reconnect 3 times. Giving up.
179 0
|
Linux
七个办法只有一个有效:200 PORT command successful. Consider using PASV.425 Failed to establish connection.
七个办法只有一个有效:200 PORT command successful. Consider using PASV.425 Failed to establish connection.
638 0
七个办法只有一个有效:200 PORT command successful. Consider using PASV.425 Failed to establish connection.
执行HQL直接被退出:Remote side unexpectedly closed network connection
执行HQL直接被退出:Remote side unexpectedly closed network connection
1222 0
执行HQL直接被退出:Remote side unexpectedly closed network connection
‘Client‘ is not allowed to run in parallel.Would you like to stop the running one?
‘Client‘ is not allowed to run in parallel.Would you like to stop the running one?
589 0
‘Client‘ is not allowed to run in parallel.Would you like to stop the running one?