官方文档 :
3
30
1000
false
test
false
100
null
false
60
3
60
15
100
3
root
password
select id from test where id=1
300
false
true
root
false
con_test
30000
30
10
30
25
10
0
200
300
转:
解决mysql 8小时问题
最近的一个项目在hibernate使用c3p0的连接池,数据库为mysql。开发测试没有问题,在运行中每个一段长的空闲时间就出现异常:
java 代码
- org.hibernate.exception.jdbcconnectionexception: could not execute query
- at org.hibernate.exception.sqlstateconverter.convert(sqlstateconverter.java:74)
- at org.hibernate.exception.jdbcexceptionhelper.convert(jdbcexceptionhelper.java:43)
- .......
- caused by: com.mysql.jdbc.exceptions.mysqlnontransientconnectionexception: no operations allowed after connection closed.connection was implicitly closed due to underlying exception/error:
- ** begin nested exception **
- com.mysql.jdbc.communicationsexception
- message: communications link failure due to underlying exception:
- ** begin nested exception **
- java.net.socketexception
- message: broken pipe
- stacktrace:
- java.net.socketexception: broken pipe
- at java.net.socketoutputstream.socketwrite0(native method)
- ......
- ** end nested exception **
查看了mysql的文档,以及connector/j的文档以及在线说明发现,出现这种异常的原因是:
mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,mysql将自动断开该 connection。这就是问题的所在,在c3p0 pools中的connections如果空闲超过8小时,mysql将其断开,而c3p0并不知道该connection已经失效,如果这时有 client请求connection,c3p0将该失效的connection提供给client,将会造成上面的异常。
解决的方法有3种:
- 增加wait_timeout的时间。
- 减少connection pools中connection的lifetime。
- 测试connection pools中connection的有效性。
当然最好的办法是同时综合使用上述3种方法,下面就dbcp和c3p0分别做一说明,假设wait_timeout为默认的8小时
dbcp增加以下配置信息:
-
- validationquery = "select 1"
-
- testwhileidle = "true"
-
- timebetweenevictionrunsmillis = 3600000
-
- minevictableidletimemillis = 18000000
-
- testonborrow = "true"
c3p0增加以下配置信息:
-
- testconnectiononcheckin = true
- //自动测试的table名称
- automatictesttable=c3p0testtable
-
- idleconnectiontestperiod = 18000
-
- maxidletime = 25000
-
- testconnectiononcheckout = true
更多的配置信息大家可以查看c3p0文档,connector/j文档,以及dbcp的文档。
转:
我自己的配置:
jdbc.driverclass=com.mysql.jdbc.driver
jdbc.jdbcurl = jdbc:mysql://localhost:3306/test
jdbc.user = root
jdbc.password = 12345
jdbc.minipoolsize = 1
jdbc.maxpoolsize = 20
jdbc.initialpoolsize = 1
jdbc.maxidletime = 25000
jdbc.acquireincrement = 1
jdbc.acquireretryattempts = 30
jdbc.acquireretrydelay = 1000
jdbc.testconnectiononcheckin = true
jdbc.automatictesttable = c3p0testtable
jdbc.idleconnectiontestperiod = 18000
jdbc.checkouttimeout=3000
posted on 2009-03-29 23:31
alpha 阅读(91650)
评论(26) 所属分类:
hibernate