Book Image

EJB 3 Developer Guide

By : Michael Sikora
Book Image

EJB 3 Developer Guide

By: Michael Sikora

Overview of this book

Table of Contents (18 chapters)
EJB 3 Developer Guide
Credits
About the Author
About the Reviewers
Preface
Annotations and Their Corresponding Packages

Examples of Transaction Attributes


We will show examples of all the transaction attributes starting with the default, REQUIRED.

REQUIRED Example

A REQUIRED annotated method is required to execute in a transaction but not necessarily a new transaction. If a transaction is already active then the method will execute in that transaction. If no transaction is active, a new transaction is started.

Suppose we have a stateless session bean, BankServiceBean, and we want to create an audit record every time we add a customer to the database by means of the addCustomer() method. Suppose this method invokes a method, addAuditMessage(), on another stateless session bean AuditServiceBean. This method creates and then persists an Audit entity. The Audit entity attributes are simply an identifier and a message string.

The normal sequence of events is:

  1. 1. start addCustomer() method

  2. 2. persist Customer entity

  3. 3. start addAuditMessage() method

  4. 4. persist Audit entity

  5. 5. end addAuditMessage() method

  6. 6. end addCustomer...