background image

Bean-Managed Transactions

<< Rolling Back a Container-Managed Transaction | JDBC or JTA transactions >>
<< Rolling Back a Container-Managed Transaction | JDBC or JTA transactions >>

Bean-Managed Transactions

The container invokes the beforeCompletion method after the business method has finished,
but just before the transaction commits. The beforeCompletion method is the last opportunity
for the session bean to roll back the transaction (by calling setRollbackOnly).
The afterCompletion method indicates that the transaction has completed. It has a single
boolean
parameter whose value is true if the transaction was committed and false if it was
rolled back.
Methods Not Allowed in Container-Managed
Transactions
You should not invoke any method that might interfere with the transaction boundaries set by
the container. The list of prohibited methods follows:
The commit, setAutoCommit, and rollback methods of java.sql.Connection
The getUserTransaction method of javax.ejb.EJBContext
Any method of javax.transaction.UserTransaction
You can, however, use these methods to set boundaries in application-managed transactions.
Bean-Managed Transactions
In bean-managed transaction demarcation, the code in the session or message-driven bean
explicitly marks the boundaries of the transaction. Although beans with container-managed
transactions require less coding, they have one limitation: When a method is executing, it can
be associated with either a single transaction or no transaction at all. If this limitation will make
coding your bean difficult, you should consider using bean-managed transactions.
The following pseudocode illustrates the kind of fine-grained control you can obtain with
application-managed transactions. By checking various conditions, the pseudocode decides
whether to start or stop different transactions within the business method.
begin transaction
...
update table-a
...
if (condition-x)
commit transaction
else if (condition-y)
update table-b
commit transaction
else
rollback transaction
begin transaction
Bean-Managed Transactions
Chapter 33 · Transactions
1003