getRollbackOnly()
Because life’s too short for a bean to waste time
Once a bean has called setRollbackOnly(), the transaction is sentenced to death. It will never commit. But the transaction might still have a long way to go, with plenty of other methods in other beans, and with lots of heavy code.
Imagine you’re a bean. How would you feel if the transaction were already doomed before your methods were called, but nobody told you?
CMT beans call getRollbackOnly() to find out if the transaction they’re in is already doomed. If the transaction is never going to commit, why should the bean waste time with lots of code?

If you’re a CMT bean, you can call getRollbackOnly() to find out if your transaction has already been sentenced to death. If it has, why bother doing any work?
if (!getRollbackOnly()) {
saveWorld();
} else {
abandonAllHope();
}Note
getRollbackOnly() is NOT for BMT beans! Only CMT beans can call getRollbackOnly()
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