Could not open Hibernate Session for transaction, 数据库连接超时解决方法

本文介绍了解决Hibernate在进行事务操作时遇到的JDBC异常的方法,包括通过修改数据库连接URL增加autoReconnect参数以及使用支持重连的连接池来提高系统的稳定性和可用性。
异常:
javax.servlet.ServletException: org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed: 
 
root cause 
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed: 
 
 
解决方案:
1.
给jdbc url 增加 autoReconnect=true 一定能解决你的问题,可以定期观察一下 show processlist
改进方法如下:
<property name="url" value="jdbc:mysql://localhost/数据库实例名
称?&useUnicode=true&characterEncoding=utf-8&autoReconnect=true"/>
 
2.
寻找支持重连的连接池。
           注意:c3p0连接池支持重连;重连参数是:
               idleConnectionTestPeriod   设置空闲连接测试周期
               preferredTestQuery : 设置一查询语句,用于重连测试
              testConnectionOnCheckin设置为true
              testConnectionOnCheckout设置为true
 
在sessionFactory里配置:
<property name="hibernateProperties">
   <props>
        <prop key="hibernate.autoReconnect">true</prop> 
  </props>
</property>

2025-10-09 16:45:15,219 [(pro) WebContainer : [Anonymous][]-2050016273-7bfcb9bfbb-43-T:adc78117-91bd-4039-8ac3-5f4bb5671e27] ERROR SsoFilter: - null org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: Cannot create PoolableConnectionFactory (FATAL: Invalid [********] at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:313) ~[spring-jdbc-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.transaction.support.AbstractPlatformTransactionManager.startTransaction(AbstractPlatformTransactionManager.java:400) ~[spring-tx-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373) ~[spring-tx-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:595) ~[spring-tx-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:382) ~[spring-tx-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175) ~[spring-aop-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:762) ~[spring-aop-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175) ~[spring-aop-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:762) ~[spring-aop-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:762) ~[spring-aop-5.3.39-h2.jar!/:5.3.39-h2] at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:707) ~[spring-aop-5.3.39-h2.jar!/:5.3.39-h2] at com.huawei.it.jalor5.security.service.impl.LoginService$$EnhancerBySpringCGLIB$$18b7c081.initUser[********] at com.huawei.it.jalor5.web.support.filter.SsoFilter.processInitUser(SsoFilter.java:174) ~[jalor-auth*****#*#*****.0.RELEASE.jar!/:[********].RELEASE] at com.huawei.it.jalor5.web.support.filter.SsoFilter.access$300(SsoFilter.java:51) ~[jalor-auth*****#*#*****.0.RELEASE.jar!/:[********].RELEASE] at com.huawei.it.jalor5.web.support.filter.SsoFilter$1.execute(SsoFilter.java:142) ~[jalor-auth*****#*#*****.0.RELEASE.jar!/:[********].RELEASE] at com.huawei.it.jalor5.core.exception.impl.LogAndThrowBox.run(LogAndThrowBox.java:25) ~[jalor-core-[********]-SP15.RELEASE.jar!/:[********]-SP15.RELEASE] at com.huawei.it.jalor5.web.support.filter.SsoFilter.doAfterSsoInfoBuilt(SsoFilter.java:114) ~[jalor-auth*****#*#*****.0.RELEASE.jar!/:[********].RELEASE] at com.huawei.his.idaas.sso.validate.UserAuth*****#*#*****er[********] at com.huawei.his.idaas.sso.validate.UserAuth*****#*#*****o[********] at com.huawei.his.idaas.sso.filter.AbstractSsoFilter.doFilter(AbstractSsoFilter.java:109) ~[sso-sdk-2.2.8.jar!/:?] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:168) ~[tomcat-embed-core-9.0.108.jar!/:?] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:144) ~[tomcat-embed-core-9.0.108.jar!/:?] at com.huawei.it.jalor5.security.filter.JwtRequestFilter.skipJwtAuth*****#*#*****er.java:176) ~[jalor-auth*****#*#*****LEASE.jar!/:[********].RELEASE] at com.huawei.it.jalor5.security.filter.JwtRequestFilter.doFilter(JwtRequestFilter.java:123) ~[jalor-auth*****#*#*****LEASE.jar!/:[********].RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:168) ~[tomcat-embed-core-9.0.108.jar!/:?] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:144) ~[tomcat-embed-core-9.0.108.jar!/:?] at com.huawei.it.jalor5.web.support.filter.RequestContextFilter.doFilter(RequestContextFilter.java:84) ~[jalor-web-[********].RELEASE.jar!/:[********].RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:168) ~[tomcat-embed-core-9.0.108.jar!/:?] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:144) ~[tomcat-embed-core-9.0.108.jar!/:?] at com.huawei.it.jalor5.web.support.filter.CharaterEncodingFilter.doFilter(CharaterEncodingFilter.java:31) ~[jalor-web-[********].RELEASE.jar!/:[********].RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:168) ~[tomcat-embed-core-9.0.108.jar!/:?这个是什么问题
10-10
评论 9
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值