关于使用C3P0之后,项目在服务器上开启事务错误的原因

笔者遇到过这个问题

使用的环境是Struts2+Hibernate+Spring4

错误代码摘取

HTTP Status 500 - Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin transaction failed:

type Exception report

message Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin transaction failed:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin transaction failed: 
	org.springframework.orm.hibernate5.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:541)

     分析错误代码之后字面意思是事务开启失败,其实问题不在于事务开启失败,错误的原因在于C3P0自己对于连接池内部各个连接的处理。
     C3P0自己的处理过程是:它连接池内的连接本是都连接这数据库,用户访问网站,框架调用数据库连接池拿取连接,操作,但是Mysql每8小时会自动断开所有连接,而C3P0并不会去检查这些连接的有效性,当有用户再次访问的时候,C3P0仍然会把池内的数据库空连接(C3P0认为是正常连接,因为它自己默认是不会去校验的)给框架调用,就是造成上述错误。
     这个问题属于Mysql的8小时问题。解决方案,笔者就不贴了,可以自行百度。笔者把错误代码贴出来,是为了让更多人知道出现这个错误代码的原因。


猜你喜欢

转载自blog.csdn.net/watsonxh/article/details/78892827