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?

image with no caption

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()

Get Head First EJB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.