How do I make (or get) a transaction?

Two ways: code it or declare it

The container manages your transactions, but you have to tell it how. You can either put transaction code in your bean class, or you can put transaction declarations in the DD. By far, the most common approach is to use the DD, because it’s simpler, supports bean reuse, and is the only way you can do transactions for entity beans. By putting transaction information in the DD instead of in code, you can deploy the same bean multiple times and get different transaction behavior each time without ever touching the code!

  1. Write transaction code in your bean.

    image with no caption

    OR

  2. Declare transactions in the DD.

    image with no caption

Note

You can’t use BOTH in the same bean! You can’t mix BMT and CMT in one bean.

You CAN have a combination of BMT and CMT beans in the same ejb-jar, but each bean must have only one transaction type. On the exam, if you see transaction code in a bean, make sure that if you see the DD for that bean, it says:

<transaction-type> Bean </transaction-type>

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.