Search code examples
springtransactional

@Transactional in spring


i don't want to rollback db update happening in method3.i tried the below approach for doing it.but it is not working,where i am wrong ? please correct me.

 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.
        }
  }

Solution

  • I would create annotation: Say @NoRollbackTransactional

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

    And use on method which i dont want to roll back.

    You may also need to turn off globalRollbackOnParticipationFailure.

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

    Also go throught this question