Results 1 to 2 of 2

Thread: Execution code after transaction

  1. #1
    Join Date
    May 2009
    Posts
    4

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •