124697
124697

Reputation: 21893

Does the rollback transactions feature only work when an exception is thrown?

I have this declaration above my class declaration. Will spring only rollback when DAOException is thrown?

@Transactional(rollbackFor = { ManagerException.class, DAOException.class }) 

If that is the case, how can I rollback a bad commit without throwing an exception. Currently my database package functions return an error message, I want to roll back and still display the error message to the user without throwing an exception and crashing the app.

Upvotes: 0

Views: 142

Answers (1)

JB Nizet
JB Nizet

Reputation: 692013

The documentation says:

You can also indicate a required rollback programmatically. Although very simple, this process is quite invasive, and tightly couples your code to the Spring Framework's transaction infrastructure:

public void resolvePosition() {
    try {
        // some business logic...
    } catch (NoProductInStockException ex) {
        // trigger rollback programmatically
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    }
}

Upvotes: 2

Related Questions