jdbc4 通信异常

发布于 2024-07-29 07:47:41 字数 1635 浏览 5 评论 0原文

我有一台运行 java 应用程序的机器,与在同一实例上运行的 mysql 实例通信。 该应用程序 使用 mysql 的 jdbc4 驱动程序。 我不断收到 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException 在随机时间。

这是整个消息。

无法打开事务的 JDBC 连接; 嵌套异常

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可以解决问题吗?

I have a machine running a java app talking to a mysql instance running on the same instance. the app
uses jdbc4 drivers from mysql. I keep getting com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
at random times.

Here is the whole message.

Could not open JDBC Connection for transaction; 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. the wait timeout value is much higher than the 26 secs(25899 msecs). mentioned in the exception trace.

I use dbcp for connection pooling and here is spring bean config for the datasource.

   <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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

且行且努力 2024-08-05 07:47:41

尝试正确设置 Apache Commons DBCP。

您需要设置:

  • validationQuery to SELECT 1+1
  • testOnBorrow to true

这应该可以解决问题。

Try setting up the Apache Commons DBCP correctly.

You need to set:

  • validationQuery to SELECT 1+1
  • testOnBorrow to true

That should fix the problem.

溇涏 2024-08-05 07:47:41

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

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. The app needs to reconnect when it loses a connection.

白鸥掠海 2024-08-05 07:47:41

我会遵循例外情况中的建议。 您应该考虑:

  1. 在应用程序中使用之前过期和/或测试连接有效性,
  2. 增加客户端超时的服务器配置值,或
  3. 使用 Connector/J 连接属性“autoReconnect=true”来避免此问题。 尝试将其添加到您的连接 URL(请参阅文档以了解确切的语法),看看是否有帮助。

我怀疑 C3P0 是否会比您已经使用的 DBCP 好得多。 例外的是给你一些具体的建议。 你已经尝试过#3。 另外两个呢?

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

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. Try adding that to your connection URL (consult the docs for the exact syntax) and see if it helps.

I doubt that C3P0 will be that much better than the DBCP that you're already using. The exception is giving you some specific advice. You've tried #3. What about the other two?

I know how to ask WebLogic to check connections before using them. You should find out how to do the same with Tomcat.

假情假意假温柔 2024-08-05 07:47:41

我以前见过在网络上移动的 Windows 计算机在连接​​到自身时遇到问题。

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

I have seen before that Windows machines which have been moved on the network have had trouble with connecting to themselves.

Is there any connectivity problems outside the JVM - i.e. mysql client connecting to the server, and timing out, etc?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文