0
votes

I am facing a problem in transaction rollback using the @Transactional annotation.

I have the following methods in backingbean, service and dao classes:

public class ItemBackingBean {
    public void saveUpdate() {
        try {
            ItemService.executeTransaction();
        }
        catch(Exception e) {
        }
    }
}

public class ItemService {
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void executeTransaction() {
        deleteItem();
        createOrder();
    }

    private void deleteItem() {
        persist();
    }

    private void createOrder() {
        persist();
    }

    private void persist() {
        JpaDaoImpl.persist(object);
        JpaDaoImpl.update(object);
    }
}

public class JpaDaoImpl implements JpaDao {
    @Transactional(readOnly = true)
    public persist(Object object) {
        getEm().persist(object);
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void update(Object object) {
        getEm().merge(object);
    }

    @Transactional(readOnly = true)
    public void remove(Object object) {
        getEm().remove(object);
    }
}

If any exception occurs in createOrder(), all transactions should rollback but it is not happening. Can any body tell me the problem?

What is the impact of @Transactional in JpaDaoImpl.java? The persist() and update() methods have different radOnly values. This Dao is existing code in our project and we don't want to change it. Can anybody help?

Regards, Bandu

2
Post your configuration and make sure that you are using a database that supports transactions (MySQL with MyISAM tables doesn't support transactions). The @Transactional on the dao basically do nothing (if your configuration is correct), the @Transactional on the service takes precedence.M. Deinum
if the db is MySQL, tables must use the InnoDB engine.vijay
My Database is Oracle. On other screen rollback in working fine.Bandu Kank

2 Answers

1
votes

For those who don't want to throw exception (transaction should NOT be only rollbacked when exception happen), use this: TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

-1
votes

If any exception occurs in createOrder(), all transactions should rollback but it is not happening. Can any body tell me the problem?

Rollback occurs only for RuntimeExceptions (see http://docs.spring.io/spring/docs/2.0.8/reference/transaction.html "please note that the Spring Framework's transaction infrastructure code will, by default, only mark a transaction for rollback in the case of runtime, unchecked exceptions;") but it is a customizable behaviour

You can keep the default transaction propagation that is PROPAGATION_REQUIRED without affecting the existing code if you want a ALL or NOTHING behaviour