C3P0连接超时

时间:2014-07-17 08:27:58

标签: java connection-pooling c3p0

我开发了一个门户网站,为用户提供了访问现有系统信息的方法。它基于Java构建,在Tomcat,Linux和MySQL上运行。用户很少访问系统。可能每周一次打开并关闭一些东西。

奇怪的是,有时,我们的系统会遇到数据库连接问题,如果用户刷新浏览器,它将再次运行。以下是发生连接问题时的堆栈跟踪:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 648,770,075 milliseconds ago.  The last packet sent successfully to the server was 648,770,076 milliseconds ago. 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.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3988)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2598)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2825)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2323)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
    at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:646)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:589)
    ... 96 more
Caused by: java.net.SocketException: Connection timed out
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)

注意,我没有更改MySQL wait_timeout,这意味着它默认为wait_timeout = 28800

我不明白的是错误提示7天左右连接处于休眠状态?错误消息:“从服务器成功收到的最后一个数据包是648,770,075毫秒”。但我想我通过不时测试连接来正确设置我的C3P0连接,设置maxConnectionAge,并通过设置testConnectionOnCheckout = true每次检查测试连接。以下是配置:

<bean id="dataSource"
    class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/SocialAction?useEncoding=true&amp;characterEncoding=UTF-8&amp;characterResultSets=utf8&amp;connectionCollation=utf8mb4_unicode_ci" />
    <property name="user" value="username" />
    <property name="password" value="password" />
    <property name="minPoolSize" value="5" />
    <property name="maxPoolSize" value="400" />
    <property name="maxIdleTime" value="120" />
    <property name="maxConnectionAge" value="7200" />
    <property name="acquireRetryAttempts" value="5" />
    <property name="maxStatements" value="100" />
    <property name="idleConnectionTestPeriod" value="60" />
    <property name="testConnectionOnCheckout" value="true" />
    <property name="preferredTestQuery" value="SELECT 1" />
    <property name="unreturnedConnectionTimeout" value="240" />
    <property name="debugUnreturnedConnectionStackTraces" value="false" />
</bean>

上面的堆栈跟踪建议使用AutoReconnect = true,但因为社区提出了不同的建议“Why does autoReconnect=true not seem to work?”。我不会走那条路

因此,百万美元的问题是,为什么在我有testConnectionOnCheckout = true和idleConnectionTestPeriod = true时会发生这种情况?我最近添加了maxIdleTime = 120,但它只是没有解决这个问题。你看到我的C3P0配置有什么问题吗?

0 个答案:

没有答案