Container-managed transactions

Now that we’ve looked at the do-it-yourself way to demarcate transactions, you’ll see how easy it is with CMT. So easy that you don’t write anything transactional in your code except maybe an occasional call to EJBContext.setRollbackOnly() or EJBContext.getRollbackOnly().

image with no caption

With CMT, transactions are started and completed (with either a commit or rollback) by the container, based solely on the deployment descriptor. You (OK, technically the Application Assembler) mark some attributes in the deployment descriptor and that’s it.

Almost.

Unless you understand exactly what the six transaction attributes are, and the implications of how different attributes interact at runtime, you won’t have a clue about whether your bean is even going to be in a transaction. Or, how big the transaction will be. Or, whether you’ve created a dangerous situation that could blowup at runtime.

Fortunately, there are only six. And the rules for how the container behaves with each of those attributes is very clear and simple.

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.