atomikos + druid 连接超时失效

Posted 不知为何就叫呵呵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了atomikos + druid 连接超时失效相关的知识,希望对你有一定的参考价值。

atomikos + druid 连接超时失效,需要多次连接才能成功。

首次连接会报异常:

2018-01-08 16:58:12 DEBUG [com.jpcar.model.dao.jpcar.AdminDao.getByName:org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:145)] ==> Parameters: root(String)
2018-01-08 16:58:12 ERROR [com.alibaba.druid.pool.DruidPooledStatement:com.alibaba.druid.pool.DruidPooledStatement.errorCheck(DruidPooledStatement.java:296)] CommunicationsException, druid version 1.1.6, jdbcUrl : jdbc:mysql://localhost:3306/jpcar?serverTimezone=PRC&useSSL=false&useUnicode=true&characterEncoding=UTF8&autoReconnect=true, testWhileIdle true, idle millis 94276, minIdle 5, poolingCount 3, timeBetweenEvictionRunsMillis 8000, lastValidIdleMillis 94276, driver com.mysql.jdbc.Driver, exceptionSorter com.alibaba.druid.pool.vendor.MySqlExceptionSorter
2018-01-08 16:58:12 ERROR [com.alibaba.druid.pool.DruidDataSource:com.alibaba.druid.pool.DruidDataSource.handleFatalError(DruidDataSource.java:1594)] discard connection
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 91,089 milliseconds ago.  The last packet sent successfully to the server was 1 milliseconds ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3556)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3456)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3897)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192)
    at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3051)
    at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
    at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3049)

而 druid 连接池也配置了

<property name="validationQuery" value="SELECT 1" />
        <property name="timeBetweenEvictionRunsMillis" value="2800000" />
        <property name="minEvictableIdleTimeMillis" value="600000" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />

 

等数据库的wait_timeout到时,依然会报错,需要多次连接才成功。

 

解决:

在配置 AtomikosDataSourceBean 时 配上 连接检查:

    <bean id="jpcarAtom" class="com.atomikos.jdbc.AtomikosDataSourceBean"
        init-method="init" destroy-method="close">
        <property name="uniqueResourceName" value="mysql/jpcar" />
        <property name="xaDataSource" ref="jpcar" />
        <property name="maintenanceInterval" value="28000" />
        <property name="testQuery" value="SELECT 1" />
    </bean>

具体解决方案

至于原理,不懂。瞎蒙到的。

估计是druid那边的连接移除了,atomikos那还有缓存。

 

以上是关于atomikos + druid 连接超时失效的主要内容,如果未能解决你的问题,请参考以下文章

Druid连接池 查询慢 超时

Druid配置参数详解-maxWait

Druid 连接池,重启Mysql数据库后,Druid会自动重新连接不,该在哪配置?

Axis2如何设置连接超时时间

mysql怎么设置超时时间

HttpClient连接池的连接保持超时和失效机制