调用第三方接口过长导致数据库连接超时( Communications link failure during rollback(). Transaction resolution unknown.)

业务逻辑是:推送按钮-处理推送逻辑-往第三方系统推送文件(大文件几十分钟)-推送完成-记录推送状态-操作完成

在推送完成往数据库写入数据的时候抛出如下异常:

com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 5,401,659 milliseconds ago. The last packet sent successfully to the server was 5,401,672 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

根据异常内容可知是数据库连接超时导致的,建议用'autoReconnect=true' ,但是数据库连接配置加了这个也没用,后面又想到用连接池加入如下配置:

druid:
  maxWait: 60000
  timeBetweenEvictionRunsMillis: 60000
  minEvictableIdleTimeMillis: 300000
  validationQuery: SELECT 1
  testWhileIdle: true
  testOnBorrow: false
  testOnReturn: false
  filters: stat,wall,log4j2

重新测试又有新的报错:

ERROR [SOpie519c97577ca] com.alibaba.druid.util.JdbcUtils 96 - close connection error java.sql.SQLNonTransientConnectionException: Communications link failure during rollback(). Transaction resolution unknown.

原因是事务导致的,所以这个推送方法还得脱离事务。

最后把推送方法脱离事务范围之外就可以了,至此长时间推送后业务持久化操作正常了。

脱离事务的方式有很多中,要看什么框架以及框架版本和配置的优先级等等。

我这边试了几种:

1、@Transactional(propagation = Propagation.NOT_SUPPORTED) 注解

2、把包名命名在配置事务处理范围外

<!-- 只对业务逻辑层实施事务 -->
<aop:pointcut id="txPointcut" expression="execution(* cn.pinming.jsgc..service..*+.*(..)) or execution(* cn.pinming.jsgc..task..*+.*(..)) or execution(* cn.pinming.jsgc..realm..*+.*(..))"/>

3、配置文件中配置:

<tx:method name="xxxxxxx" propagation="NOT_SUPPORTED"/>
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值