setRollbackOnly()
The sound of a transaction’s death
When a bean calls setRollbackOnly() it means that transaction is going down. Once you invoke setRollbackOnly(), the transaction is doomed to never, ever commit.

So, what does it mean to sentence a transaction to death? It means the transaction definitely won’t commit. Duh. But it also means that any participant in the transaction (i.e. any bean) can check to see if the transaction is already marked for death.
Remember, a transaction started by a BMT bean might propagate to method calls that the BMT bean makes on CMT beans. A CMT bean in a BMT-started transaction might want to sentence the transaction to death, or at least find out whether it’s already doomed.
In a CMT bean, setRollbackOnly() is how you tell the container that it must not commit the transaction. If you can figure out in your business logic that a transaction is going to end badly, call setRollbackOnly(). The container won’t end the transaction at that point, but when it does end at its natural time, it definitely won’t commit.
Note
It’s tragic.
When you call the setRollbackOnly() method, you set a flag that can tell other beans that the transaction will end only one way... horribly.
If you know, in your code, that things aren’t going to work, call this method.
If the transaction is going to die, it’s probably for the best. It was meant to be. It’s going to a better place. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access