问题描述
我有一个事务服务,该服务调用另一个具有REQUIRES_NEW传播的服务.
I have a transactional service calling a second one with a REQUIRES_NEW propagation.
激活日志我看到即使创建了当前线程的Session,Spring仍会在创建子事务时创建一个新的Hibernate Session.
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?
此行为在org.springframework.orm.hibernate3.HibernateTransactionManager中进行了硬编码:
This behavior is hardcoded in org.springframework.orm.hibernate3.HibernateTransactionManager:
xObject.getSessionHolder().setSynchronizedWithTransaction(true);
我正在使用休眠3.6和Spring 3.1
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
这篇关于Spring可以/应该将Hibernate会话重用于子事务(REQUIRES_NEW)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!