background image

Container-Managed Transactions

<< Managing Distributed Transactions | Using the JMS API with Application Clients >>
<< Managing Distributed Transactions | Using the JMS API with Application Clients >>

Container-Managed Transactions

Distributed transactions within the EJB container can be either of two kinds:
Container-managed transactions
: The EJB container controls the integrity of your
transactions without your having to call commit or rollback. Container-managed
transactions are recommended for Java EE applications that use the JMS API. You can
specify appropriate transaction attributes for your enterprise bean methods.
Use the Required transaction attribute (the default) to ensure that a method is always part
of a transaction. If a transaction is in progress when the method is called, the method will be
part of that transaction; if not, a new transaction will be started before the method is called
and will be committed when the method returns.
Bean-managed transactions
: You can use these in conjunction with the
javax.transaction.UserTransaction
interface, which provides its own commit and
rollback
methods that you can use to delimit transaction boundaries. Bean-managed
transactions are recommended only for those who are experienced in programming
transactions.
You can use either container-managed transactions or bean-managed transactions with
message-driven beans. To ensure that all messages are received and handled within the context
of a transaction, use container-managed transactions and use the Required transaction
attribute (the default) for the onMessage method. This means that if there is no transaction in
progress, a new transaction will be started before the method is called and will be committed
when the method returns.
When you use container-managed transactions, you can call the following
MessageDrivenContext
methods:
setRollbackOnly
: Use this method for error handling. If an exception occurs,
setRollbackOnly
marks the current transaction so that the only possible outcome of the
transaction is a rollback.
getRollbackOnly
: Use this method to test whether the current transaction has been marked
for rollback.
If you use bean-managed transactions, the delivery of a message to the onMessage method takes
place outside the distributed transaction context. The transaction begins when you call the
UserTransaction.begin
method within the onMessage method, and it ends when you call
UserTransaction.commit
or UserTransaction.rollback. Any call to the
Connection.createSession
method must take place within the transaction. If you call
UserTransaction.rollback
, the message is not redelivered, whereas calling setRollbackOnly
for container-managed transactions does cause a message to be redelivered.
Neither the JMS API specification nor the Enterprise JavaBeans specification (available from
http://java.sun.com/products/ejb/
) specifies how to handle calls to JMS API methods
outside transaction boundaries. The Enterprise JavaBeans specification does state that the EJB
container is responsible for acknowledging a message that is successfully processed by the
onMessage
method of a message-driven bean that uses bean-managed transactions. Using
Using the JMS API in a Java EE Application
Chapter 31 · The Java Message Service API
959