Why Bean-Managed Transactions?

EJB with bean-managed transactions is the programmatic approach in which the EJB code invokes methods that mark the boundaries of the transaction. On Day 17, you learned about container-managed transactions. CMT is the declarative approach in transaction demarcation in which the container manages the transaction behind the scenes on behalf of the EJB, and according to the configuration of the EJB's deployment descriptor. You also learned that container-managed transaction is the approach that J2EE encourages you to use.

You might ask yourself why you should study bean-managed transactions. The answer is simple: Sometimes a container-managed transaction is not sufficient or adequate to perform certain transactional tasks you are trying to accomplish. For example, a method of your EJB must be associated with either a single transaction or no transaction at all. In this situation, due to the limitations of container-managed transactions to set these conditions in the EJB's deployment descriptor, you might consider using bean-managed transactions.

Enterprise applications use BMT to perform fine-grained control of transaction boundaries in business methods. For situations in which distributed transactions are managing multiple resources, bean-managed transaction may also be used. The following pseudo-code illustrates the workflow of a business method controlling transaction boundaries across multiple resource managers. Based on certain conditions, the code decides whether to begin a new transaction or end different transactions within the same method.

businessMethod(...)
...
  begin transaction
   ...
   update database tableA
   send message to a queueA
   ...
   if (conditionA)
       send message to dead-letter queue
       commit transaction
   else
       rollback transaction
       begin transaction
           update database tableA
           send another message to queueB
           commit transaction
   end if
....
end method

Traditionally, transactional applications are responsible for managing their transactions. These applications demand a very skilled developer to perform tasks such as creating transaction objects, explicitly starting a transaction, keeping track of the transaction context, and committing the transaction when all updates have completed. The EJB architecture provides the BMT for advanced developers to carry on the same traditions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.9.197