c3p0 mysql数据库连接池 8小时联接失效

c3p0 mysql数据库连接池 8小时连接失效

mysql 8 小时默认就断开了不适用的连接。但是c3p0  默认 连接是不断开的。所以就会8小时后不访问项目的话,第一次登陆汇报一下异常。

 

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 56,846,765 milliseconds ago.  The last packet sent successfully to the server was 56,846,766 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.

参考文档:

http://blog.sina.com.cn/s/blog_62cb15980102uzid.html

http://www.blogjava.net/Alpha/archive/2009/03/29/262789.html

http://www.cqmaple.com/201308/no-operations-allowed-after-connection-closed.html

 

最后贴出我的配置

 

<!-- 数据库链接池c3p0 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClassName}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

		<property name="maxPoolSize" value="${c3p0.pool.size.max}" />
		<property name="minPoolSize" value="${c3p0.pool.size.min}" />
		<property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
		<property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
		
		<property name="testConnectionOnCheckin" value="${c3p0.pool.testConnectionOnCheckin}" /> 
		<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable等方法来提升连接测试的性能。Default: false -->
		<!--<property name="testConnectionOnCheckout" value="true" />  -->
<!-- 		<property name="automaticTestTable" value="${c3p0.pool.automaticTestTable}"></property> -->
		<property name="preferredTestQuery"><value>SELECT 1</value></property>
  		<property name="idleConnectionTestPeriod" value="${c3p0.pool.idleConnectionTestPeriod}" />
  		<property name="maxIdleTime" value="${c3p0.pool.maxIdleTime}" />
	</bean>
#When not using JMX, eliminate many times load c3p0 warnings
com.mchange.v2.c3p0.management.ManagementCoordinator=com.mchange.v2.c3p0.management.NullManagementCoordinator

c3p0.pool.size.max=20
c3p0.pool.size.min=10
c3p0.pool.size.ini=5
c3p0.pool.size.increment=3

c3p0.pool.testConnectionOnCheckin=true
#c3p0.pool.automaticTestTable=c3p0automaticTestTable
c3p0.pool.idleConnectionTestPeriod=1800
c3p0.pool.maxIdleTime=25000