When are Connections Returned to the Connection Pool with Spring JPA (Hibernate) Entity Manager?
Understanding the Process:
The Spring transaction manager acts as an abstraction for transaction management, while actual transactions occur at the JDBC Connection level.
-
Transaction Interceptor:
When a @Transactional method is called, it is intercepted by the TransactionInterceptor Aspect.
-
Transaction Management:
The TransactionInterceptor delegates transaction management to the configured AbstractPlatformTransactionManager implementation (JpaTransactionManager in this case).
-
Entity Manager Binding:
JpaTransactionManager binds the current transaction to an EntityManager, ensuring that all DAOs involved in the transaction share the same Persistence Context.
-
Transaction Control:
JpaTransactionManager uses the EntityManager Transaction API to control transactions.
-
Transaction Completion:
Upon transaction completion (commit/rollback), the JdbcTransaction calls managedClose(), triggering Session and JDBC Connection closure.
-
Connection Handling by DataSource:
The DataSourceConnectionProvider (configured for HikariCP in this example) closes the JDBC Connection, returning it to the connection pool.
Note on RESOURCE_LOCAL Transactions:
For RESOURCE_LOCAL transactions, set the hibernate.connection.provider_disables_autocommit property to ensure lazy connection acquisition.
The above is the detailed content of Here are a few question-based titles that fit your article content:
* How Do Spring JPA and Hibernate Handle Connection Pool Return?
* When Are Connections Released Back to the Pool in Spring JPA Tra. For more information, please follow other related articles on the PHP Chinese website!