Announcement Announcement Module
Collapse
No announcement yet.
@Transactional rollback and AOP Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Transactional rollback and AOP

    Hi,
    I have a method that I use @Transactional

    @Transactional
    public void save(MyObject myObject) {
    ....
    }


    When a rollback occurs, I want my program to do something before and/or after rollback. This is my AOP code.

    @Aspect
    public class MyAspect {

    @Pointcut("execution(* *..MyObjectManagerImpl.save(..))")
    public void save() {}

    @After("save()")
    public void afterSave(){
    System.out.println("afterSAVE");
    }

    @Before("save()")
    public void beforeSave(){
    System.out.println("beforeSAVE");
    }




    @Pointcut("execution(* *..JDBCTransaction.rollback(..))")
    public void rollback() {}


    @After("rollback()")
    public void afterRollback(){
    System.out.println("afterROLLBACK");
    }

    @Before("rollback()")
    public void beforeRollback(){
    System.out.println("beforeROLLBACK");
    }
    }


    When I test my code, it print the save advices but NOT the rollback's however. While my log file indicates that rollback did happen.

    MessageListenerThreadPool : 1 2010-11-29 12:10:24,399 org.hibernate.transaction.JDBCTransaction DEBUG line 152 -rollback
    MessageListenerThreadPool : 1 2010-11-29 12:10:24,415 org.hibernate.transaction.JDBCTransaction DEBUG line 193 -re-enabling autocommit
    MessageListenerThreadPool : 1 2010-11-29 12:10:24,415 org.hibernate.transaction.JDBCTransaction DEBUG line 163 -rolled back JDBC Connection


    Can anyone explain to me what is wrong and how should I fix it? Thanks!

  • #2
    org.hibernate.transaction.JDBCTransaction is not a Spring bean defined in the context. It is not managed by Spring directly but used internally by Hibernate.
    Spring AOP is only applied to beans of the context (objects that Spring recognizes and can deal with directly) so it only makes sense that advices are not applied to JDBCTransaction.

    To achieve what you want to do you can't use Spring AOP but need something like AspectJ's load time weaving instead; I, however, strongly advise you not to do this.

    The reason is simple: if you let Spring handle transactions for you (using the tx: namespace and the @Transactional annotations) you should let Spring handle it completely and not mess around by adding logic to commits and rollbacks: unless you perfectly know what's behind Spring transaction management, there's a good chance you'll cause more harm than good.

    Comment


    • #3
      And more

      Please have a look at chapter 10.5.8 of the current spring documentation.
      There it says, how you can advice a transactional method and object respectively. It should give you a hint on how to check whether or not transactional methods succeeded to commit or not (AfterThrowing Advice for instance).
      If you combine chapter 10.5.8 with this forum thread you will end up with a best practice solution.
      Hope this helps.

      Comment

      Working...
      X