gpt4 book ai didi

java - jdbc4 通信异常

转载 作者:可可西里 更新时间:2023-11-01 06:30:26 27 4
gpt4 key购买 nike

我有一台运行 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 会解决问题吗?

最佳答案

尝试正确设置 Apache Commons DBCP。

你需要设置:

  • SELECT 1+1 的验证查询
  • testOnBorrow 为真

这应该可以解决问题。

关于java - jdbc4 通信异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1152124/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com