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

来源:互联网 发布:中国大数据企业排行榜 编辑:程序博客网 时间:2024/05/16 14:09
ssh项目,服务器开了一夜后,出现了这样的异常,在博客上找到了这样的解决方案,
[java] view plaincopy
  1. 异常:  
  2. javax.servlet.ServletException: org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed:   
  3.    
  4. root cause   
  5. org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed:   
  6.    
  7.    
  8. 解决方案:  
  9. 1.  
  10. 给jdbc url 增加 autoReconnect=true 一定能解决你的问题,可以定期观察一下 show processlist  
  11. 改进方法如下:  
  12. <property name="url" value="jdbc:mysql://localhost/数据库实例名  
  13. 称?&useUnicode=true&characterEncoding=utf-8&autoReconnect=true"/>  
  14.    
  15. 2.  
  16. 寻找支持重连的连接池。  
  17.            注意:c3p0连接池支持重连;重连参数是:  
  18.                idleConnectionTestPeriod   设置空闲连接测试周期  
  19.                preferredTestQuery : 设置一查询语句,用于重连测试  
  20.               testConnectionOnCheckin设置为true  
  21.               testConnectionOnCheckout设置为true  
  22.    
  23. 在sessionFactory里配置:  
  24. <property name="hibernateProperties">  
  25.    <props>  
  26.         <prop key="hibernate.autoReconnect">true</prop>   
  27.   </props>  
  28. </property>  
0 0
原创粉丝点击