Hibernate c3p0 Mysql 配置注意

MySQL默认是自动关闭空闲超过8小时的连接,而C3P0并不知道该connection已经失效,如果这时有Client请求connection,C3P0将该失效的Connection提供给Client,将会造成异常。重启Tomcat操作会使C3P0重新建立和数据库的连接池,且池内的连接都是有效的连接。
解决方法:c3p0要在8小时内关闭不使用的连接。这需要修改其中的一些配置参数,主要是maxIdleTime和idleConnectionTestPeriod。这两个参数的值要小于28800秒。
建议配置如下:
<!-- 每次都验证连接是否可用 -->
<property name="hibernate.c3p0.validate">true</property>
<!-- 检查连接池中所有空闲连接的间隔时间,单位为秒(要注意的是MySQL的自动关闭空闲超过8小时连接的机制) -->
<property name="hibernate.c3p0.idle_test_period">18000</property>
<!-- 连接的最大空闲时间,25000秒内未使用则连接被丢弃。单位为秒(要注意的是MySQL的自动关闭空闲超过8小时连接的机制) -->
<property name="hibernate.c3p0.timeout">25000</property>


附上在 hibernate中配置c3p0的关键字。 

c3p0.acquireIncrement           hibernate.c3p0.acquire_increment 
c3p0.idleConnectionTestPeriod   hibernate.c3p0.idle_test_period 
c3p0.maxIdleTime                hibernate.c3p0.timeout 
c3p0.maxPoolSize                hibernate.c3p0.max_size 
c3p0.maxStatements              hibernate.c3p0.max_statements 
c3p0.minPoolSize                hibernate.c3p0.min_size 





“SEVERE: The last packet successfully received from the server was144382 seconds ago.The last packet sent successfully to the server was 144382 seconds ago, which  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.” 
你的log里说的很清楚了。原因是Mysql服务器默认的“wait_timeout”是8小时,如果一个connection空闲超过8个小时,Mysql将自动断开该 connection,而C3P0并不知道该connection已经失效,如果这时请求connection,将会造成上面的异常。你每次使用前判断connection是否有效就会避免这种异常。 

解决的方法有3种: 

增加wait_timeout的时间。 
减少Connection pools中connection的lifetime。 
测试Connection pools中connection的有效性。 
当然最好的办法是同时综合使用上述3种方法,下面举个例子,假设wait_timeout为默认的8小时 

C3P0增加以下配置信息: 

//获取connnection时测试是否有效 
testConnectionOnCheckin = true 
//自动测试的table名称 

automaticTestTable=C3P0TestTable 

//set to something much less than wait_timeout, prevents connections from going stale 
idleConnectionTestPeriod = 18000 
//set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out 
maxIdleTime = 25000 
//if you can take the performance 'hit', set to "true" 
testConnectionOnCheckout = true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值