user manual

204 BES Developers Guide
Declarative transaction management in Enterprise JavaBeans
EJBs utilize a declarative style of transaction management that differs from the
traditional transaction management style. With declarative management, the
EJB declares its transaction attributes at deployment time. The transaction
attributes indicate whether the EJB container manages the bean's
transactions or whether the bean itself manages its own transactions, and, if
so, to what extent it does its own transaction management.
Traditionally, the application was responsible for managing all aspects of a
transaction. This entailed such operations as:
Creating the transaction object.
Explicitly starting the transaction.
Registering resources involved in the transaction.
Keeping track of the transaction context.
Committing the transaction when all updates completed.
It requires a developer with extensive transaction processing expertise to write
an application that is responsible for managing a transaction from start to
finish. The code for such an application is more complex and difficult to write,
and it is easy for "pilot error" to occur.
With declarative transaction management, the EJB container manages most if
not all aspects of the transaction for you. The EJB container handles starting
and ending the transaction, plus maintains its context throughout the life of the
transaction object. This greatly simplifies an application developer's
responsibilities and tasks, especially for transactions in distributed
environments.
Understanding bean-managed and container-managed
transactions
When an EJB programmatically performs its own transaction demarcation as
part of its business methods, that bean is considered to be using bean-
managed transaction. On the other hand, when the bean defers all transaction
demarcation to its EJB container, and the container performs the transaction
demarcation based on the Application Assembler's deployment instructions,
then the bean is referred to as using container-managed transaction.
EJB session beans, both stateful and stateless varieties, can use either
container- or bean-managed transactions. However, a bean cannot use both
types of transaction management at the same time. EJB entity beans can only
use container-managed transaction. It is the bean provider who decides the
type of transaction which an EJB can use.
An EJB can manage its own transaction if it wishes to start a transaction as
part of one operation and then finish the transaction as part of another
operation. However, such a design might be problematic if one operation calls
the transaction starting method, but no operation calls the transaction ending
method.