hibernate 使用连接池连接数据库时 自动断开处理

阅读更多

项目中使用c3p0连接数据库里 ,每隔一段时间就报以下错误:

JDBC begin failed [ERROR] 2011-05-08 01:56:33 com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** java.net.SocketException MESSAGE: Connection timed out STACKTRACE: java.net.SocketException: Connection timed out at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(Unknown Source) at java.net.SocketOutputStream.write(Unknown Source) at java.io.BufferedOutputStream.flushBuffer(Unknown Source)

..................

at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source) Caused by: org.hibernate.TransactionException: JDBC begin failed: at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:68) at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326) at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:558) ... 89 more Caused by: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

错误内容太多 ,中间省略很大一部分..

datasource配置:

几经周折,发现使用的mysql数据库有一个系统参数为

wait-timeout The number of seconds the server waits for activity on a noninteractive connection before closing it.

看网上说的默认时间是8小时(但是我使用的mysql版本配置文件中没有发现有此属性配置,也不知道默认的是不是8小时,姑且当作是),所以在连接池配置中添加了以下配置不再出现异常:



注意此时间要小于数据库配置的空闲等待时间

你可能感兴趣的:(hibernate 使用连接池连接数据库时 自动断开处理)