Announcement Announcement Module
Collapse
No announcement yet.
Execution code after transaction Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Execution code after transaction

    Hi.
    I have the following problem: I need executed some code after transaction was commited. For example:
    Code:
    class MyManager {
         @Transactional
         public void doSomething() {
              // Code required transaction
              myDao.doSomethingWihDao(); 
              
             // Code MUST by executed after transaction
             listeners.fireEvent();
         }
    }
    The main problem that transaction created outside of method doSomething() but events which fired in this method should be fired after transaction.
    I can't start new transaction here because active session locks some data (for example lazy collections in Hibernate).

    I think it will be good have something like:
    Code:
    class MyManager {
         @Transactional
         public void doSomething() {
              // Code required transaction
              myDao.doSomethingWihDao(); 
              
             // Code MUST by executed after transaction
             fireEvent();
         }
    
         @AfterTransactionCommited
         protected void fireEvent() {
             listeners.fireEvent();
         }
    }
    or something like:

    Code:
    class MyManager {
         @Transactional
         public void doSomething() {
              // Code required transaction
              myDao.doSomethingWihDao(); 
    
             TransactionUtil.afterTransactionCommited(new Runnable()) { 
                    public void run() {
                      // Code MUST by executed after transaction
                     listeners.fireEvent();
                   }
             }
         }
    }

  • #2
    1) Create an aspect which runs after the transaction (use an after advice or if it is only for succesful transactions a after-returning advice)
    2) Use the TransactionSynchronization to execute the listeners.

    I would suggest 1 because that is a bit simpler to implement and create.

    Comment

    Working...
    X