c3p0连接池使用问题

前段使用C3P0连接池连接数据库,其中遇到一问题,服务器每过一段时间便会报错:

2014-09-04 17:08:18 ERROR [resin-port-8080-30902] [TrendTouchServiceImpl] TrendTouchService uploadPhotoTrend error: org.springframework.dao.RecoverableDataAccessException: PreparedStatementCallback; SQL [**** ]; Communications link failure

Last packet sent to the server was 2 ms ago.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

查了一下,这个问题是因为连接池中的连接失效,而应用直接拿来使用,没有对连接进行检查造成的,解决方案:

在连接池配置中加入以下两个参数:

<property name="testConnectionOnCheckout" value="true"/>
<property name="preferredTestQuery" value="select 1"/>

问题解决! 

猜你喜欢

转载自haiziwoainixx.iteye.com/blog/2119628
今日推荐