[记录]JPA/Hibernate的MySQL长连接问题

 

问题:web app使用JPA配置persistence.xml连接MySQL做持久化,JPA使用的是Hibernatejar包,web app在过了一夜之后不能正常工作,出现空指针异常SEVERE: Servlet.service() for servlet jsp threw exception java.lang.NullPointerException
出事的persistence.xml如下:

 

 

 

RootCause

MySQL defaultwait_timeoutinteractive_timeout 时间为8小时,如果没有对JDBC Connection Pool设置的话,超过这个时间数据库连接池的连接就会被MySQL断开。

 

 

这里跟这个问题相关的两个参数是
wait_timeout : MySQL
关闭没有活动的连接的等待时间(秒)
interactive_timeout
MySQL关闭有交互的连接的等待时间(秒)
这个情况下可以这么理解,JDBC连接池初始化了20个连接,其中15个还没有实际的交互,5个有数据库操作正在进行。
wait_timeout
作用的是这15个连接,MySQL等待8小时发现这15个连接依旧没有交互的话,就关闭这些连接。
interactive_timeout
作用的是5个有交互的连接,从最后一次交互结束算起MySQL等待8小时发现这5个连接没有继续有交互活动的话就关闭这个连接。
解决办法:在persistence.xml中增加配置connection pool的属性参数

 

 

c3p0hibernate用到的开源的数据库连接池,下面是主要参数的解释

  • hibernate.c3p0.min_size This is the minimum number of JDBC connections that C3P0 keeps ready at all times
  • hibernate.c3p0.max_size This is the maximum number of connections in the pool. An exception is thrown at runtime if this number is exhausted.
  • hibernate.c3p0.timeout You specify the timeout period (in this case, 300 seconds) after which an idle connection is removed from the pool).
  • hibernate.c3p0.max_statements Maximum Number of statements that will be cached. Caching of prepared statements is essential for best performance with Hibernate.
  • hibernate.c3p0.idle_test_periods This is the iddle time in seconds before a connection is automatically validated.

 

参考:
http://www.taobaodba.com/html/433_mysql_timeout_analyze.html
http://stackoverflow.com/questions/3123962/hibernate-and-mysql-timeout-problem-doesnt-work-with-c3p0
http://stackoverflow.com/questions/475893/which-are-the-required-c3p0-settings-for-hibernate-in-order-to-avoid-deadlocks

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值