总结hibernate报错及排错记录,避免两次掉进同一个坑。
1、我的tiny网站经常在刚上线时很正常,过一两天之后访问会高频刷出500:hibernateTransactionException:rollback failed。查看详细的报错信息,还有这个(下面两行是在stackoverflow截的,但是信息除了时间具体数值其他都是一样的):
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 79,547,948 milliseconds ago. The last packet sent successfully to the server was 79,547,964 milliseconds ago. 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.
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
查找网上资源发现有一个说法是说得通的:mysql超过一定时间未进行数据库操作,tomcat会断开数据库连接。(我的小网站是在部门内部用来给员工登记几条记录的,几个小时内没有select等数据库操作是很正常的)。
然后就是怎么解决这个问题:配置c3p0数据库连接池参数(在此之前,先导入hibernate的lib/optional/c3p0下的几个jar包,我用的版本是hibernate 4.3.11,jar包有3个:c3p0-0.9.2.1.jar、hibernate-c3p0-4.3.11.Final.jar、mchange-commons-java-0.2.3.4.jar):
<!-- 当连接池中的连接耗尽时,一次性增加的连接数量,默认为3 -->
<property name="hibernate.c3p0.acquire_increment">2</property>
<!-- 每个多少秒检测连接是否可正常使用 -->
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_size">30</property>
<!-- statements缓存大小 -->
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.timeout">90</property>
<property name="hibernate.c3p0.validate">true</property>

本文探讨了Hibernate在使用过程中遇到的连接超时错误,并详细介绍了如何通过配置c3p0数据库连接池来解决该问题。
858

被折叠的 条评论
为什么被折叠?



