1. ホーム
  2. ハイバーネート

トランザクションのためにHibernateセッションを開くことができず、データベース接続タイムアウトの解決法

2022-02-28 02:44:55
Exception.
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 
CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org. TransactionException: JDBC begin failed: 
 
 
Solution.
1.
Add autoReconnect=true to the jdbc url will definitely solve your problem, you can regularly observe the show processlist
Improve the method as follows.
<property name="url" value="jdbc:mysql://localhost/database instance name
name? &useUnicode=true&characterEncoding=utf-8&autoReconnect=true"/>
 
2.
Find a connection pool that supports reconnection.
           Note: The c3p0 connection pool supports reconnection; the reconnection parameter is :
               idleConnectionTestPeriod set idle connection test period
               preferredTestQuery : Set a query statement for reconnection testing
              testConnectionOnCheckin is set to true
              testConnectionOnCheckout is set to true
 
Configure in sessionFactory.
<property name="hibernateProperties">
   <props>
        <prop key="hibernate.autoReconnect">true</prop> 
  </props>
</property>