jdbc4 CommunicationsException

时间:2009-07-20 07:30:17

标签: java mysql jdbc timeout connection

我有一台运行java应用程序的机器与在同一实例上运行的mysql实例交谈。该应用程序  使用mysql的jdbc4驱动程序。我一直得到com.mysql.jdbc.exceptions.jdbc4.CommunicationsException  在随机的时间。

这是整个信息。

无法打开JDBC Connection进行事务处理;嵌套异常是

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was25899 milliseconds ago.The last packet sent successfully to the server was 25899 milliseconds ago, which  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.

对于mysql,全局'wait_timeout'和'interactive_timeout'的值设置为3600秒,'connect_timeout'设置为60秒。等待超时值远远高于26秒(25899毫秒)。在异常跟踪中提到。

我使用dbcp进行连接池,这里是数据源的spring bean配置。

   <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" >
          <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/db"/>
                <property name="username" value="xxx"/>
                <property name="password" value="xxx" />
                    <property name="poolPreparedStatements" value="false" />
            <property name="maxActive" value="3" />
            <property name="maxIdle" value="3" />
    </bean>

知道为什么会这样吗?用c3p0会解决问题吗?

4 个答案:

答案 0 :(得分:6)

尝试正确设置Apache Commons DBCP。

您需要设置:

  • validationQuery to SELECT 1 + 1
  • testOnBorrow to true

这应该解决问题。

答案 1 :(得分:1)

您能描述一下您的应用如何处理连接池吗?我怀疑JDBC驱动程序中的autoReconnect = true会重新汇集来自应用程序的连接。应用程序在失去连接时需要重新连接。

答案 2 :(得分:0)

我会遵循异常中的建议。你应该考虑:

  1. 在您的应用程序中使用之前到期和/或测试连接有效性,
  2. 增加服务器配置的客户端超时值,或
  3. 使用Connector / J连接属性'autoReconnect = true'来避免此问题。尝试将其添加到您的连接URL(请参阅文档以获取确切的语法),看看它是否有帮助。
  4. 我怀疑C3P0会比你已经使用的DBCP好得多。例外是给你一些具体的建议。你试过#3。那么另外两个呢?

    我知道如何让WebLogic在使用它们之前检查连接。您应该了解如何使用Tomcat执行相同操作。

答案 3 :(得分:0)

我之前看到,已经在网络上移动的Windows机器在连接自己时遇到了麻烦。

JVM之外是否存在连接问题 - 即连接到服务器的mysql客户端,超时等等?

相关问题