[alibaba/druid]druid 1.2.6 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException 提示druid版本1.2.5

2025-11-10 759 views
7

druid 1.2.6 配置参数如下:

initialSize: 1,
maxActive: 5,
minIdle: 1,
maxWait: 2000,
timeBetweenEvictionRunsMillis: 60000,
minEvictableIdleTimeMillis: 300000,
testOnBorrow: false,
testOnReturn: false,
testWhileIdle: true,
validationQuery: "SELECT 1",

keepAlive: true 和 false 均配过,效果一样,经常遇到CommunicationsException: Communications link failure 诡异的是提示信息说druid版本是1.2.5,但堆栈信息显示的druid包名是1.2.6:

CommunicationsException, druid version 1.2.5, jdbcUrl : jdbc:mysql://x.x.x.x:3306/db_xxx?rewriteBatchedStatements=true&allowMultiQueries=true&useLocalSessionState=true&useUnicode=true&characterEncoding=utf-8, testWhileIdle true, idle millis 30789, minIdle 1, poolingCount 0, timeBetweenEvictionRunsMillis 60000, lastValidIdleMillis 30789, driver com.mysql.jdbc.Driver, exceptionSorter com.alibaba.druid.pool.vendor.MySqlExceptionSorter
ERROR DruidDataSource discard connection
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 30,789 milliseconds ago.  The last packet sent successfully to the server was 30,788 milliseconds ago.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_262]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_262]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_262]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_262]
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.40.jar!/:5.1.40]
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989) ~[mysql-connector-java-5.1.40.jar!/:5.1.40]
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3556) ~[mysql-connector-java-5.1.40.jar!/:5.1.40]
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3456) ~[mysql-connector-java-5.1.40.jar!/:5.1.40]
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3897) ~[mysql-connector-java-5.1.40.jar!/:5.1.40]
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524) ~[mysql-connector-java-5.1.40.jar!/:5.1.40]
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677) ~[mysql-connector-java-5.1.40.jar!/:5.1.40]
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549) ~[mysql-connector-java-5.1.40.jar!/:5.1.40]
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861) ~[mysql-connector-java-5.1.40.jar!/:5.1.40]
        at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192) ~[mysql-connector-java-5.1.40.jar!/:5.1.40]
        at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:497) ~[druid-1.2.6.jar!/:1.2.6]
        at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:64) ~[mybatis-3.5.4.jar!/:3.5.4]
        at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79) ~[mybatis-3.5.4.jar!/:3.5.4]
        at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63) ~[mybatis-3.5.4.jar!/:3.5.4]
        at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324) ~[mybatis-3.5.4.jar!/:3.5.4]
        at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) ~[mybatis-3.5.4.jar!/:3.5.4]
        at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109) ~[mybatis-3.5.4.jar!/:3.5.4]
        at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83) ~[mybatis-3.5.4.jar!/:3.5.4]
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147) ~[mybatis-3.5.4.jar!/:3.5.4]
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) ~[mybatis-3.5.4.jar!/:3.5.4]
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:76) ~[mybatis-3.5.4.jar!/:3.5.4]
        at sun.reflect.GeneratedMethodAccessor193.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_262]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_262]
        at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426) ~[mybatis-spring-2.0.4.jar!/:2.0.4]
        at com.sun.proxy.$Proxy139.selectOne(Unknown Source) ~[?:?]
        at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:159) ~[mybatis-spring-2.0.4.jar!/:2.0.4]

回答

3

druid1.2.6输出日志的版本号写1.2.5是因为com.alibaba.druid.VERSION没有同步修改,但是pom.xml、druid-spring-boot-starter/pom.xml、druid-wrapper/pom.xml里还是1.2.5就可能带来麻烦了。

5

CommunicationsException 原因找到了,是读取异地机房数据库耗时超过timeout了。

6

我也碰到一样的问题,我是本机的数据库,没理由timeout,目前没找到解决的办法,后面的小伙伴要是看到了有解决办法,麻烦回复我下,万分感谢

7

我也碰到一样的问题,我是本机的数据库,没理由timeout,目前没找到解决的办法,后面的小伙伴要是看到了有解决办法,麻烦回复我下,万分感谢

本机的问题应该容易分析,1是检查数据库服务的日志,2是用arthas工具查使用数据库的应用,定位执行缓慢的环节。