Tomcat JDBC 连接池 + MySQL 出现“Broken pipe”问题,即使进行连接验证也是如此。

6

我正在尝试配置Tomcat JDBC连接池以实现可靠性。当前问题是在测试环境中,我的web应用程序出现以下情况:

  • 第1天:一切都正常
  • 第2天:网站无法与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>

我的连接URL中没有autoReconnect=true参数,只有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

1
@SanjayRajjadi 不是这样的。这是一个配置问题。请阅读消息。'Broken pipe' 大多是应用程序协议问题。 - user207421
@SanjayRajjadi MySQL正在同一台机器上,连接到本地主机。 - Piotr Müller
2个回答

5

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

maxAge="180000" 
testOnBorrow="true" 
testWhileIdle="true"
validationInterval="0" //forces the connection pool to validate each time a connection is given to the application

这些选项应该定义在哪里?我想我正在遇到同样的问题... - E. V. d. B.
您需要在数据源的 XML 配置中添加这些选项,就像原始问题所示。我只是添加了我们用于方便参考的值。您需要使用 <property name="whatever" value="somevalue"> 格式才能捕获设置。 - geoand
啊,是的,我也想到了,但显然这似乎不是我现在正在苦恼的问题 ;) 无论如何,还是感谢您回复了我的消息! - E. V. d. B.
@E.V.d.B. 没问题! - geoand

4

您需要将“testOnBorrow”设置为“true”,并且可能将“maxAge”设置为小于服务器配置的“wait_timeout”,如消息中所示。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接