Spring hibernate , how to call some method after transaction commit or transaction rollback

前端 未结 3 1815
心在旅途
心在旅途 2020-12-05 19:59

I need to call some method after transaction succes or rollback. I am using as

    

        
相关标签:
3条回答
  • 2020-12-05 20:08
    • From Hibernate, you could extends EmptyInterceptor and override afterTransactionCompletion() method and register it in SessionFactoryBean or HibernateTransactionManager.

    • From Spring you could extends TransactionSynchronizationAdapter and override afterCompletion() and register when appropriate with TransactionSynchronizationManager#registerSynchronization().

    Edit

    An Example of using Spring Aop to add a synchronization to all methods annotated with @Transactional

    @Aspect
    class TransactionAspect extends TransactionSynchronizationAdapter {
    
        @Before("@annotation(org.springframework.transaction.annotation.Transactional)")
        public void registerTransactionSyncrhonization() {
            TransactionSynchronizationManager.registerSynchronization(this);
    
        }
    
        @Override
        public void afterCompletion(int status) {
            // code 
        }
    }
    
    0 讨论(0)
  • 2020-12-05 20:10

    Spring has various classes which might be of interest here:

    http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/transaction/support/TransactionSynchronization.html

    http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/transaction/support/TransactionSynchronizationAdapter.html

    http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/transaction/support/TransactionSynchronizationManager.html

    There's some example code here:

    http://azagorneanu.blogspot.co.uk/2013/06/transaction-synchronization-callbacks.html


    Update 2016

    The event handling infrastructure introduced in Spring 4.2 makes this much simpler.

    See:

    https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2#transaction-bound-events

    Another popular improvement is the ability to bind the listener of an event to a phase of the transaction. The typical example is to handle the event when the transaction has completed successfully

    @Component
    public class MyComponent {
    
      @TransactionalEventListener(condition = "#creationEvent.awesome")
      public void handleOrderCreatedEvent(CreationEvent<Order> creationEvent) { 
        ...
      }
    
    }
    

    @TransactionalEventListener is a regular @EventListener and also exposes a TransactionPhase, the default being AFTER_COMMIT. You can also hook other phases of the transaction (BEFORE_COMMIT, AFTER_ROLLBACK and AFTER_COMPLETION that is just an alias for AFTER_COMMIT and AFTER_ROLLBACK).

    0 讨论(0)
  • 2020-12-05 20:22

    Using Spring 4+: The easiest/cleanest way without using global aspects and configurations is based on my answer here: https://stackoverflow.com/a/43322052/986160

    If you need a callback on a @Transactional method after it successfully commits just add that in the beginning of the method:

    @Service
    public class OneService {
    
        @Autowired
        OneDao dao;
    
        @Transactional
        public void a transactionalMethod() {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter(){
                public void afterCommit(){
                    //do stuff right after commit
                    System.out.println("commit!!!");
    
                }
            });
            //do db stuff
            dao.save();
        }
    }
    
    0 讨论(0)
提交回复
热议问题