未提交事务直接返回,导致连接池资源耗尽

错误分析:

我所撰写的代码如下,在使用编程式事务的时候,我未提交或回滚事务就直接返回,因为连接池默认连接数的大小是10,所以到第十一次的时候连接池就会使用完,导致任务无法进行。

@Autowired
private NstExpressScheduleplanMapper scheduleplanMapper;
@Autowired
private PlatformTransactionManager transactionManager;    

public CommonResult testTransactionDefinition() {
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        TransactionStatus status = transactionManager.getTransaction(def);
        scheduleplanMapper.getRandomAgent();

        return CommonResult.success();
}

接口无法给予响应。 

 解决方法:

 在代码内设置finally模块,确保一定提交或者回滚,代码修改如下:

@Autowired
private NstExpressScheduleplanMapper scheduleplanMapper;
@Autowired
private PlatformTransactionManager transactionManager;    

public CommonResult testTransactionDefinition() {
       TransactionStatus status = null;
        try {
            DefaultTransactionDefinition def = new DefaultTransactionDefinition();
            status = transactionManager.getTransaction(def);
            scheduleplanMapper.getRandomAgent();
            transactionManager.commit(status);
        }catch (Exception e){
            transactionManager.rollback(status);
        }finally {
            transactionManager.rollback(status);

        }

        return CommonResult.success();
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小海海不怕困难

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值