简体   繁体   中英

How to call non-transactional methods from a Transactional method

I have a method that is Transactional and I have a private method that is non-transactional that does some db update.

I need to call this private method from the transactional method but in case of exception the updates from non-transactional are also rolled back.

How can I made the non transactional method to commit the update from private method because it is called from transactional method and in case of exception its changes are also rolled back

@Transactional
public Result createTransaction(){
    try{
         //do someting
      }Catch (Exception e){
         markAsFailed();
         throw new CtxException("customer.notfound");
       }
}


 private void markAsFailed(){
        //do db update
       }

Just putting up the answer in case if someone needs, the important point to note is the Transactional Method that needs to be called from a non Transactional method should be in separate spring bean as mentioned by JB Nizet because spring works with proxies to achieve this behaviour and will have to define the method in another class so that spring should be able to trigger the transactions. Once you are inside a class you will not go through the proxy again

需要从非事务方法调用的事务方法应该在单独的spring bean中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM