0

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.
        }
  }
abhishekgarg
  • 164
  • 1
  • 4
  • 11
  • `flagclass.method2() ` did you mean `class2.method2()`? – Boris Treukhov Jul 16 '15 at 17:50
  • Read the answer to the duplicate question carefully. In particular: *As you observed, through, the proxy mechanism only works when calls come in from some external object. When you make an internal call within the object, you're really making a call through the "this" reference, which bypasses the proxy.* – JB Nizet Jul 16 '15 at 17:55
  • yeah ,it is class2.method2(). – abhishekgarg Jul 16 '15 at 18:26
  • @user3825429 It's perfectly valid to use custom transaction management if it's required - that is described in [Programmatic transaction management](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html#transaction-programmatic) – Boris Treukhov Jul 16 '15 at 21:52

1 Answers1

0

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

Community
  • 1
  • 1
VedantK
  • 9,728
  • 7
  • 66
  • 71