Given that both updates should succeed or be rolled back, while is the best solution?

A developer implements a CMT session bean with a method storeBoth which inserts data both a
related database and an LDAP server. The relational database supports transactions while the
LDAP system does NOT.
Given that both updates should succeed or be rolled back, while is the best solution?

A developer implements a CMT session bean with a method storeBoth which inserts data both a
related database and an LDAP server. The relational database supports transactions while the
LDAP system does NOT.
Given that both updates should succeed or be rolled back, while is the best solution?

A.
Implement the SessionSynchoronization interface in the session bean. In the afterCompleteion
method, the LDAP inserts are rolled back if false is passed as an argument to the afterCompletion
method.

B.
Define the transaction attribute of the method storeBoth as REQUIRED. The container
manages the transactions and will roll back modifications if something goes wrong in either
database insert or LDAP insert.

C.
Define the transaction attribute of the method storeBoth as REQUIRED_NEW. Carry out the
database insert first. Subsequently, execute the LDAP inserts, catching LDAP exceptions. If
exceptions are raised, call the SessionContext.setRollBackOnly method.

D.
Define the transaction attribute of the method storeBoth as REQUIRED_NEW. Carry out the
LDAP insert first. If SessionContext.getRollBackOnly returns false, execute the database inserts,
catching SQL exceptions. If exceptions are raised, call the SessionContext.setRollBackOnly.

Explanation:
The method should start a new transaction, so we use
theREQUIRED_NEWattribute.
For the LDAP operation we can only detect LDAP exceptions. We cannot check the status of the
LDAP operation throughSessionContext.getRollBackOnly.
Note:
* CMT -Container-Managed Transactions
*RequiresNew Attribute
If the client is running within a transaction and invokes the enterprise beans method, the container
takes the following steps:
Suspends the clients transaction
Starts a new transaction
Delegates the call to the method
Resumes the clients transaction after the method completes
If the client is not associated with a transaction, the container starts a new transaction before
running the method.
You should use the RequiresNew attribute when you want to ensure that the method always runs
within a new transaction.
Reference:The Java EE 5 Tutorial,Container-Managed Transactions



Leave a Reply 0

Your email address will not be published. Required fields are marked *