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

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

目录
相关文章
|
6月前
|
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...
167 0
|
3月前
|
数据库连接 网络安全 数据库
Could not create connection to database server.Attempted reconnect 3 times. Giving up.
这篇文章提供了解决数据库连接问题的方法,建议在连接字符串后添加`?serverTimezone=UTC`来指定时区,并检查网络设置、连接属性、驱动版本以及是否需要SSH或SSL连接。
Could not create connection to database server.Attempted reconnect 3 times. Giving up.
|
NoSQL Redis
Lettuce: Connection to x.x.x.x not allowed. This connection point is not known in the cluster view
Lettuce: Connection to x.x.x.x not allowed. This connection point is not known in the cluster view
169 0
|
6月前
|
负载均衡 Java 应用服务中间件
Client not connected, current status:STARTING
Client not connected, current status:STARTING
555 1
|
算法 Java
22-大厂面试题:Con-current Mode Failure如何导致以及解决
上文我们已经介绍了CMS垃圾收集器的工作原理以及流程,本篇我们接着深入说明CMS垃圾收集器的缺点以及所导致的一些问题应该如何解决。
267 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.
160 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.
619 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
1216 0
执行HQL直接被退出:Remote side unexpectedly closed network connection