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

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

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"/>
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐