@Transactional spring

@Transactional in spring

我不想回滚发生在 method3.i 中的数据库更新尝试了以下方法来做 it.but 它不起作用,我哪里错了? 请指正。

 import org.springframework.transaction.annotation.Transactional;
 @service  
 class1{

       @Autowired
       Class2 class2 ;

       @Transactional(propagation = Propagation.REQUIRED)
       method1(){
           //do some work
           //some check, on the basis of it which may throw exception.

           MyResponseObject obj =  flagclass.method2() ;

            //do some work on the basis of obj,
           //some condition on basis it may throw Exception.
       }

}


 @Service
 class2{
        public MyResponseObject method2(){
        MyResponseObject obj ;
        //do some work
        if(condition) {
            method3()
           throw new Exception();
           }
        return obj ;
        }

        @Transactional(propagation = Propagation.REQUIRES_NEW)
        method3(){
            //do some update in db.
        }
  }

我会创建注解:说@NoRollbackTransactional

@Transactional(noRollbackFor = RuntimeException.class)
public @interface NoRollbackTransactional {
}

并使用我不想回滚的方法。

您可能还需要关闭 globalRollbackOnParticipationFailure。

<property name="globalRollbackOnParticipationFailure" value="false" />

还要完成这个question