Like we mentioned earlier in this chapter, by default all EJB methods are automatically wrapped in a transaction. This default behavior is known as container-managed transactions, since transactions are managed by the EJB container. Application developers may also choose to manage transactions themselves, which can be accomplished by using bean-managed transactions. Both of these approaches are discussed in the following sections.
Because EJB methods are transactional by default, we run into an interesting dilemma when an EJB method is invoked from client code that is already in a transaction. How should the EJB container behave? Should it suspend the client transaction, execute its method in a new transaction, and then resume the client transaction? Should it not create a new transaction and execute its method as part of the client transaction? Should it throw an exception?
By default, if an EJB method is invoked by client...