commit changes in try-catch inside @Transactional

前端 未结 1 598
遇见更好的自我
遇见更好的自我 2020-12-19 20:33

Consider I have a method doing some stuff and logging mechanism in ActiveRecord pattern style:

@Transactional
public void doSmt() {
    try {
        someOpe         


        
相关标签:
1条回答
  • 2020-12-19 21:12

    Firstly, calling flush() is not going to do you any good: flush() does not commit anything and, as you are logging the error in the same transaction, the insert will be rolled back.

    You therefore need to start a new 'nested' transaction to log the error.

    public class A {
    
        @Autowired
        private B b;
    
        @Transactional
        public void doSmt() {
            try {
                someOperation(); // can throw exception
                b.logIntoDb(); // if everything is OK
            } catch {Exception e} {
                b.logIntoDbWithException(e.getMessage()); // log error into db
    
                throw new MyCustomException(e);
            }
        }
    }
    
    public class B{
    
        //outer transaction is still active
        public  void logIntoDb(String message) {
            final LogEntry logEntry = new LogEntry();
            logEntry.setDate(new Date());
            logEntry.setMessage(message);
            logEntry.persist();
        }
    
        // 'outer' transaction will be suspended until this method completes
        // this new transaction will commit/rollback independently of the outer transaction
        @Transactional(propagation = Propagation.REQUIRES_NEW)
        public  void logIntoDbWithException(String message) {
            final LogEntry logEntry = new LogEntry();
            logEntry.setDate(new Date());
            logEntry.setMessage(message);
            logEntry.persist();
        }
    }
    
    0 讨论(0)
提交回复
热议问题