Search code examples
springhibernateaop

Spring AOP ordering - Transactions before Advise


I am writing some Advice and I want the Transactions to commit before it gets to the advice. It seems to work for Save and Delete, but when I update, it runs thorough the advice, and then throws the exception.

try {
    retVal = pjp.proceed();
} catch (Exception e) {
    PSPSFaultException fault = new PSPSFaultException(e);
    pmLog.error(ERR_MSG, fault);
    throw fault;
}

As you can see here, I am trying to Wrap the exceptions in our own Runtime exception.

I tried ordering:

<tx:annotation-driven transaction-manager="txManager" order="1"/>

and

@Around( "execution(* blah.blah.PersistenceProvider.*(..))")
@Order(value=2)
public Object persistenceWrapper(ProceedingJoinPoint pjp) throws Throwable {

but this does not seem to make any difference in the stack or functionality.

@Transactional(propagation = Propagation.REQUIRED)
public class PersistenceProviderImpl extends HibernateDaoSupport implements  PersistenceProvider {

@Override
public void update(Object o) {
    this.getHibernateTemplate().update(o);
}

Is there a way to get the update to fire before the advice? The transactions are at the dao level...


Solution

  • Ok, so you are doing 2 things wrong:

    1. You should have smaler value near your advices and bigger value near your transaction manager (higher priority for advices, executed first - lower priority for transaction manager, executed later)
    2. Yous should put @Order(1) annotation in front of whole @Aspect class (strange, but it works for me)

    Something like this:

    ...
    <tx:annotation-driven transaction-manager="txManager" order="200"/>  
    ...
    @Order(1)
    @Aspect
    public class MyAspect {
    ...