[英]REQUIRES_NEW not creating a new transaction in spring+hibernate
I have a Spring and hibernate application (both latest version) and I have 2 beans as mentioned below 我有一个Spring和hibernate应用程序(均为最新版本),并且有两个bean,如下所述
@Component
public class Bean1{
@Autowired
Bean2 bean2;
@Transactional(propagation = Propagation.REQUIRED)
public void foo()
{
bean2.bar();
}
@Component
public class Bean2{
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void bar()
{
try{
// Do something which throws exception
}
catch (Exception e) {
log & eat the exception here only.
But inspite of this the outer transaciton gets rolled back
}
}
The issue is that when bean2.bar
causes any exception (eg foreign Key ConstraintViolationException
) then it rolls back the outer transaction as well saying " Transaction rolled back because it has been marked as rollback-only","moreInfo":""}" 问题是,当
bean2.bar
导致任何异常(例如,外Key ConstraintViolationException
)时,它还会回滚外部事务,并说“事务已回滚,因为它已被标记为仅回滚”,“ moreInfo”:“”}“
On seeing hibernate logs I found only one line for "new transaction" 在查看休眠日志时,我仅发现一行用于“新交易”
D| o.s.o.h.HibernateTransactionManager- Creating new transaction with name ... PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
which means no new transaction is getting created for the inner bean2.bar();
这意味着不会为内部
bean2.bar();
创建新的事务bean2.bar();
I am not able to find out what's wrong here? 我无法在这里找出问题所在? Any help is greatly appreciated.
任何帮助是极大的赞赏。
REQUIRES_NEW applies to JTA transaction Managers only. REQUIRES_NEW仅适用于JTA事务管理器。 Refer Spring Doc here
在这里参考Spring Doc
REQUIRES_NEW
REQUIRES_NEW
public static final Propagation REQUIRES_NEW
Execute non-transactionally, suspend the current transaction if one exists.
以非事务方式执行,如果当前事务存在,则挂起当前事务。 Analogous to EJB transaction attribute of the same name.
类似于同名的EJB事务属性。
Note: Actual transaction suspension will not work on out-of-the-box on all transaction managers.注意:实际的事务挂起不能在所有事务管理器上开箱即用。 This in particular applies to JtaTransactionManager, which requires the javax.transaction.TransactionManager to be made available it to it (which is server-specific in standard J2EE).
这尤其适用于JtaTransactionManager,它要求使javax.transaction.TransactionManager对其可用(在标准J2EE中服务器特定)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.