即使使用连接验证,Tomcat JDBC Conencton Pool + MySQL 也会出现“断管”问题

Pio*_*ler 5 java mysql tomcat connection-pooling jdbc

我正在努力配置 Tomcat JDBC 连接池以实现可靠性。当前的问题是,在测试环境中,我在 webapp 中有这样的 scanerio:

  • 第 1 天:一切正常
  • 第 2 天:webapp 无法与 MySQL 通信几个小时,日志中有很多“断管”
  • 第 3 天:令人惊讶的是,一切又恢复正常(无需输入或重新启动)

我已经配置了validationInterval, validationQuery, validationTimeout. 这是我的数据源配置:

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="username" value="${dbUser}" />
    <property name="password" value="${dbPass}" />
    <property name="url" value="${dbUrl}" />
    <property name="defaultAutoCommit" value="false" />
    <property name="defaultTransactionIsolation">
    <util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE" />
    </property>

    <property name="maxActive" value="300" />
    <property name="maxIdle" value="25" />
    <property name="initialSize" value="5" />

    <property name="validationInterval" value="5000" />
    <property name="validationQuery" value="SELECT 1"/>
    <property name="validationQueryTimeout" value="3" />

    <property name="minIdle" value="5" />
    <property name="initSQL" value="SET time_zone = '+00:00';" />
</bean>
Run Code Online (Sandbox Code Playgroud)

autoReconnect=true在连接 URL 中没有参数,只有 UTF8 编码。

确切的错误是:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
The last packet successfully received from the server was 38,700,615
milliseconds ago.  The last packet sent successfully to the server was
38,700,615 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.
Caused by: java.net.SocketException: Broken pipe
Run Code Online (Sandbox Code Playgroud)

geo*_*and 5

我们的一个应用程序遇到了一些类似的问题,经过大量挖掘,我们添加了以下属性来解决我们所有的连接问题:

maxAge="180000" 
testOnBorrow="true" 
testWhileIdle="true"
validationInterval="0" //forces the connection pool to validate each time a connection is given to the application
Run Code Online (Sandbox Code Playgroud)