I have a transactional service calling a second one with a REQUIRES_NEW propagation.
Activating logs I see that Spring creates a new Hibernate Session when creating the sub-transaction, even though there is already an open Session for the current Thread.
[DEBUG] org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource.getTransactionAttribute(AbstractFallbackTransactionAttributeSource.java:106) Adding transactional method 'executeProcessed' with attribute: PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT; ''
[DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager.doGetTransaction(HibernateTransactionManager.java:437) Found thread-bound Session [org.hibernate.impl.SessionImpl@467743d7] for Hibernate transaction
[DEBUG] org.springframework.transaction.support.AbstractPlatformTransactionManager.handleExistingTransaction(AbstractPlatformTransactionManager.java:415) Suspending current transaction, creating new transaction with name [com.microsigns.ds.businesslogic.rendering.service.impl.PipelineDataServiceImpl.executeProcessed]
[DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:493) **Opened new Session** [org.hibernate.impl.SessionImpl@3d4b1aad] for Hibernate transaction
[DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:504) Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@3d4b1aad]
[DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:569) Exposing Hibernate transaction as JDBC transaction [jdbc:postgresql://localhost:5432/ds, UserName=microsignsdds, PostgreSQL Native Driver]
Shouldn’t it be better performance-wise to reuse the session? This would also avoid having to reload or re-attach objects in the sub-transaction. What am I missing here?
This behavior is hardcoded in org.springframework.orm.hibernate3.HibernateTransactionManager:
xObject.getSessionHolder().setSynchronizedWithTransaction(true);
I am using hibernate 3.6 and Spring 3.1
No it should not reuse a session. If you require a new transaction then you need to be able to modify , commit , rollback etc everything in the sub transaction. An hibernate session can be thought as transaction cache , that hold the transaction data