druid 曝出No operations allowed after connection closed 解决方案

spring mysql 文章 2021-11-12 15:46 2405 0 全屏看文

AI助手支持GPT4.0

今日执行batchInsert的时候爆出这些异常。

2021-11-12 14:40:19.539 [DubboServerHandler-10.202.93.188:20880-thread-107-1030475787230321664] WARN  o.s.j.support.SQLErrorCodesFactory 220 - Error while extracting database name - falling back to empty error codes
org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
	November 12th 2021, 14:40:19.435	message:2021-11-12 14:40:19.427 [DubboServerHandler-10.202.93.188:20880-thread-107-1030475787230321664] DEBUG druid.sql.Statement 134 - {conn-10010, pstmt-20000} closed @timestamp:November 12th 2021, 14:40:19.435 @version:1 host:distribution-serial-center-686bc4bdb5-vvsc9 docker.labels.annotation.io.kubernetes.container.hash:56e08b21 docker.labels.io.kubernetes.pod.namespace:scrm docker.labels.io.kubernetes.container.name:distribution-serial-center _id:ewjgEn0BJ_hYMHX7Jy0q _type:doc _index:logstash-release-2021.11.12 _score: -

 java.sql.SQLException: Could not retrieve transaction read-only status from server at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:878) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:874) at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3523) at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3490) at com.mysql.jdbc.PreparedStatement.executeBatchInternal(PreparedStatement.java:1287) at

意思是占用连接太长被释放了。

在分析代码和处理逻辑后,疑似是执行下面代码里数据太多造成的。

jdbcTemplate.batchUpdate(sql, batchArgs);

当然,我觉得不一定是超时,也有可能是批量插入数据太多造成了执行sql时发生了异常造成的。

毕竟在日常操作时,发现有的时候SQL写错 也可能会造成 mysql的connection closed。

在把几万条数据分批执行batchUpdate后,正常执行了。

-EOF-

AI助手支持GPT4.0