关于使用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小时问题。解决方案,笔者就不贴了,可以自行百度。笔者把错误代码贴出来,是为了让更多人知道出现这个错误代码的原因。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值