user manual

Chapter 19: Transaction management 205
Declarative transaction management in Enterprise JavaBeans
Whenever possible, enterprise beans should use container-managed
transactions as opposed to bean-managed transactions. Container-managed
transactions require less programming work and are less prone to
programming error. In addition, a container-managed transaction bean is
easier to customize and compose with other beans.
Local and Global transactions
A local transaction is a transaction that is managed by the resource manager
such as that associated with a database. A global transaction, on the other
hand, is a transaction managed by the Transaction Manager or the OTS
engine which are global transaction managers.
An EJB that uses bean-managed transaction demarcation uses the
javax.transaction.UserTransaction interface to explicitly demarcate global
transaction boundaries. When transaction demarcation is container managed,
the container interposes each client call to control the transaction
demarcation. It then controls this demarcation declaratively, according to the
transaction attribute set by the Application Assembler in the deployment
descriptor. The transaction attribute also determines whether the transaction
is local or global.
The EJB container follows certain rules to determine when it is to do a local
versus a global transaction for container-managed transactions. In general, a
container calls the method within a local transaction after verifying that no
global transaction already exists. It also verifies that it is not expected to start a
new global transaction and that the transaction attributes are set for container-
managed transactions. The container automatically wraps a method
invocation within a local transaction if one of the following is true:
If the transaction attribute is set to NotSupported and the container detects
that database resources were accessed.
If the transaction attribute is set to Supports and the container detects that
the method was not invoked from within a global transaction.
If the transaction attribute is set to Never and the container detects that
database resources were accessed.
The EJB container supports the following characteristics for local transactions:
Local transactions support the javax.ejb.EJBContext methods
setRollbackOnly() and getRollbackOnly().
Local transactions support time-outs for database connections and
transactions.
Local transactions are lightweight from a performance standpoint.
The EJB container supports the following characteristics for global
transactions:
Local transactions support time-outs for database connections and
transactions.
Local transactions are lightweight from a performance standpoint.