微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – jdbc4 CommunicationsException

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

解决方法:

尝试正确设置Apache Commons DBCP.

你需要设置:

> validationQuery to SELECT 1 1
> testOnBorrow为true

这应该解决问题.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐