简体   繁体   English

REQUIRES_NEW在Spring + Hibernate中不创建新交易

[英]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.

相关问题 Spring事务REQUIRED与REQUIRES_NEW:回滚事务 - Spring transaction REQUIRED vs REQUIRES_NEW : Rollback Transaction Spring真的用REQUIRES_NEW开始新的交易吗? - Does Spring actually start a new transaction with REQUIRES_NEW? Spring可以/应该将Hibernate会话重用于子事务(REQUIRES_NEW) - Can/should Spring reuse Hibernate Session for sub-transaction (REQUIRES_NEW) Spring 事务从 NOT_SUPPORTED 到 REQUIRES_NEW 的传播 - Spring Transaction Propagation from NOT_SUPPORTED to REQUIRES_NEW 在 Spring Boot 的 for 循环中使用 REQUIRES_NEW 的事务问题 - Transaction problem using REQUIRES_NEW in a for loop with Spring Boot Spring交易REQUIRES_NEW是否会传播到方法中的方法? - Will Spring transaction REQUIRES_NEW be propagated to the methods within the method? 休眠/春季-JUnit失败,出现事务性错误(REQUIRES_NEW) - Hibernate/Spring - JUnit fails with Transactional(REQUIRES_NEW) 为什么不提交 Requires_New 的事务? - Why does not commit transaction of Requires_New? Sprint事务-如果内部事务失败,则REQUIRES_NEW行为 - Sprint transaction - REQUIRES_NEW behavior if the inner transaction fails REQUIRES_NEW:Spring 外部事务回滚,而不是在内部回滚时持久化 - REQUIRES_NEW: Spring outer transaction rollbacks also instead of persisting when inner rollbacks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM