Sammy Pawar
Sammy Pawar

Reputation: 1271

Spring @Transactional rollbackFor not working

I have a code like below

   public abstract class AffltTransactionService implements IAffltTransactionService {
    ....

        @Override
        @Transactional
        public void processTransactions(List<? extends AffltTransaction> transactions) {
            for (AffltTransaction transaction : transactions) {
                if (transaction != null) {
                    processTransaction(transaction);
                }
            }
        }

        private void processTransaction(AffltTransaction transaction) {
            try {
                processTransactionInternal(transaction);

            } catch (Exception exception) {
                affltTransactionError = new AffltTransactionError(null, null, "error", new Date());
                saveAffltTransactionError(affltTransactionError);
                log.error(exception.getLocalizedMessage());
            }
        }

        @Transactional(readOnly=false, rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
        public void processTransactionInternal(AffltTransaction transaction) {

processTransactionInternal throws ServiceUnAvailableException which extends RuntimeException

But the transaction is not getting rolled back despite having rollbackFor = Exception.class . Can you please help.

Upvotes: 2

Views: 15379

Answers (4)

Karpinski
Karpinski

Reputation: 157

I know it was asked a long time ago but I faced with the same issue and for me was missing Spring configurantion annotation:

@EnableTransactionManagement

After write it on ApplicationConfiguration class it was solved. I hope it helps someone on future.

Upvotes: 1

Nagendra Gupta
Nagendra Gupta

Reputation: 11

Use getCurrentSession instead of opensession

Upvotes: 1

Artem Bilan
Artem Bilan

Reputation: 121550

Since you invoke one method from another within the same bean, the Spring AOP doesn't use any advices in this case.

Only processTransactions is wrapped with TransactionInteceptor.

To make it worked you should configure:

<aop:aspectj-autoproxy expose-proxy="true"/>

But it isn't recommened, though.

More info here: http://www.intertech.com/Blog/secrets-of-the-spring-aop-proxy

Upvotes: 2

Vladimir
Vladimir

Reputation: 9753

@Transactional annotation won't have any effect if you are calling the method directly, since Spring creates proxies above annotated classes and the aspect-defined functionality is implemented by proxy. So, when you call the method from within your class it doesn't get through proxy, and hence no transcation is created and/or rolled back.

Take a look at the Spring reference for detailed explanation.

Upvotes: 11

Related Questions