简体   繁体   English

jdbc4 CommunicationsException

[英]jdbc4 CommunicationsException

I have a machine running a java app talking to a mysql instance running on the same instance. 我有一台运行java应用程序的机器与在同一实例上运行的mysql实例交谈。 the app uses jdbc4 drivers from mysql. 该应用程序使用mysql的jdbc4驱动程序。 I keep getting com.mysql.jdbc.exceptions.jdbc4.CommunicationsException at random times. 我随机时间不断收到com.mysql.jdbc.exceptions.jdbc4.CommunicationsException。

Here is the whole message. 这是整个信息。

Could not open JDBC Connection for transaction; 无法打开JDBC Connection进行事务处理; nested exception is 嵌套异常是

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.

For mysql, the value of global 'wait_timeout' and 'interactive_timeout' is set to 3600 seconds and 'connect_timeout' is set to 60 secs. 对于mysql,全局'wait_timeout'和'interactive_timeout'的值设置为3600秒,'connect_timeout'设置为60秒。 the wait timeout value is much higher than the 26 secs(25899 msecs). 等待超时值远远高于26秒(25899毫秒)。 mentioned in the exception trace. 在异常跟踪中提到。

I use dbcp for connection pooling and here is spring bean config for the datasource. 我使用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>

Any idea why this could be happening? 知道为什么会发生这种情况吗? Will using c3p0 solve the problem ? 请问使用c3p0解决问题?

Try setting up the Apache Commons DBCP correctly. 尝试正确设置Apache Commons DBCP。

You need to set: 你需要设置:

  • validationQuery to SELECT 1+1 validationQuery to SELECT 1 + 1
  • testOnBorrow to true testOnBorrow为true

That should fix the problem. 这应该解决问题。

Can you describe how your app is handling connection pooling? 您能描述一下您的应用如何处理连接池吗? I doubt that autoReconnect=true in the JDBC driver would re-pool connections from your app. 我怀疑JDBC驱动程序中的autoReconnect = true会重新汇集来自应用程序的连接。 The app needs to reconnect when it loses a connection. 当应用程序丢失连接时,应用程序需要重新连接。

I'd follow the advice in the exception. 我会遵循异常中的建议。 You should consider either: 你应该考虑:

  1. expiring and/or testing connection validity before use in your application, 在您的申请中使用之前到期和/或测试连接有效性,
  2. increasing the server configured values for client timeouts, or 增加服务器配置的客户端超时值,或
  3. using the Connector/J connection property 'autoReconnect=true' to avoid this problem. 使用Connector / J连接属性'autoReconnect = true'来避免此问题。 Try adding that to your connection URL (consult the docs for the exact syntax) and see if it helps. 尝试将其添加到您的连接URL(请参阅文档以获取确切的语法),看看它是否有帮助。

I doubt that C3P0 will be that much better than the DBCP that you're already using. 我怀疑C3P0会比你已经使用的DBCP好得多。 The exception is giving you some specific advice. 例外是给你一些具体的建议。 You've tried #3. 你试过#3。 What about the other two? 那两个呢?

I know how to ask WebLogic to check connections before using them. 我知道在使用它们之前如何让WebLogic检查连接。 You should find out how to do the same with Tomcat. 您应该了解如何使用Tomcat执行相同的操作。

I have seen before that Windows machines which have been moved on the network have had trouble with connecting to themselves. 我之前看到,已经在网络上移动的Windows机器在连接到自己时遇到了麻烦。

Is there any connectivity problems outside the JVM - ie mysql client connecting to the server, and timing out, etc? JVM之外是否存在任何连接问题 - 即连接到服务器的mysql客户端,以及超时等?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM