Use JTA to control transaction demarcation

Chapter 2. Design, build and test web components Chapter 2. Design, build and test web components Test and debug enterprise components Describe the effects of a server failure on the application Interact with connection pools to obtain and release connections Part II. Appendixes WSAD 5.0 Practicing for IBM Test 000-287 Preface Identify misbehaving application components Implement mechanisms which support loose coupling between clients and components Manipulate transactional behavior of EJBs using deployment descriptors Use JTA to control transaction demarcation Design, develop and test JSPs Configure JMS connection factories and destinations Implement Java clients calling Web Services Chapter 3. Develop clients that access the enterprise components IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide Implement mechanisms for efficient inter-component calls Chapter 7. Validate, tune and troubleshoot an application within an IBM WebSphere Application Server environment Manage end-user state and understand performance tradeoffs of using HTTP sessions Explain implications of resource management on application design and implementation Part I. Exam Objectives Access container and server services from enterprise components Chapter 1. Design, build and test reusable enterprise components Validate operational parameters of application server to support the enterprise application Design and develop entity EJBs Design and develop message-driven EJBs Chapter 5. EJB transactions Chapter 4. Demonstrate understanding of database connectivity and messaging within IBM WebShpere Application Server Design and develop custom tags Configure resource and security-role references Chapter 6. Assemble enterprise applications and deploy them in IBM WebSphere Application Server Deploy enterprise applications into servers WSAD 5.0 Practicing for IBM Test 000-287 Mind Map Appendix A. Additional materials

Use JTA to control transaction demarcation

This page discusses - Use JTA to control transaction demarcation

This page discusses - Use JTA to control transaction demarcation

Use JTA to control transaction demarcation

In a bean-managed transaction (BMT), the code in the session or message-driven bean explicitly marks the boundaries of the transaction. An entity bean CANNOT have bean-managed transactions, it must use container-managed transactions. When you code a bean-managed transaction for session or message-driven beans, you typically can use JTA transactions.

For JTA transactions, you can invoke the begin(), commit(), and rollback() methods of the javax.transaction.UserTransaction interface. The begin() and commit() methods mark the transaction boundaries. If the transaction operations fail, an exception handler typically invokes the rollback() method, and throws an EJBException. The following code shows how to use the JTA javax.transaction.UserTransaction interface to perform a bean-managed transaction:

UserTransaction ut = context.getUserTransaction();

try {
	// Do whatever transaction functionality is necessary
} catch (Exception ex) {
	try {
	} catch (SystemException syex) {
		throw new EJBException("Rollback failed: " + syex.getMessage());
	throw new EJBException("Transaction failed: " + ex.getMessage());

In a stateless session bean with bean-managed transactions, a business method MUST commit or roll back a transaction before returning. However, a stateful session bean does not have this restriction. In a stateful session bean with a JTA transaction, the association between the bean instance and the transaction is retained across multiple client calls. Even if each business method called by the client opens and closes the database connection, the association is retained until the instance completes the transaction.

There is one method limitation with JTA bean-managed transactions: do NOT invoke the getRollbackOnly() and setRollbackOnly() methods of the EJBContext interface interface in bean-managed transactions (BMT), these methods should be used ONLY in container-managed transactions (CMT). For bean-managed transactions, invoke the getStatus() and rollback() methods of the UserTransaction interface instead. And, be sure not to invoke any resource-specific functions that conflict with the transactional semantics.

Using component-managed (bean-managed) transactions

NOTE: Entity Beans CANNOT manage transactions (so CANNOT use bean-managed transactions).

To enable a session bean, servlet, or application client component to use component-managed transactions, complete the following steps:

  1. For Session Beans, set the Transaction type attribute in the component's deployment descriptor to Bean.

    Transaction type
  2. For Application Client components, enable support for transaction demarcation by setting the Allow JTA Demarcation attribute in the component's deployment descriptor.

  3. Write the component code to actively manage transactions.

    For Stateful Session Beans, a transaction started in a given method does not need to be completed (that is, committed or rolled back) before completing that method. The transaction can be completed at a later time, for example on a subsequent call to the same method, or even within a different method. However, constructing the application so a transaction is begun and completed within the same method call is usually preferred, because it simplifies application debugging and maintenance.

    The following code extract shows the standard code required to obtain an object encapsulating the transaction context, and involves the following basic steps:

    • A javax.transaction.UserTransaction object is created by calling a lookup on "java:comp/UserTransaction".

    • The UserTransaction object is used demarcate the boundary of a transaction by using transaction methods such as begin and commit as needed. If an application component begins a transaction, it must also complete that transaction either by invoking the commit method or the rollback method.

      Code example: Getting an object that encapsulates a transaction context:

      public float doSomething()throws NamingException {
      	InitialContext initCtx = new InitialContext();
      	UserTransaction userTran = (UserTransaction)initCtx.lookup(
      	//Use userTran object to call transaction methods
      	//Do transactional work

package javax.transaction;

import java.lang.IllegalArgumentException;
import java.lang.IllegalStateException;
import java.lang.SecurityException;

 * The UserTransaction interface defines the methods that allow an
 * application to explicitly manage transaction boundaries.
public interface UserTransaction {

	void begin() throws NotSupportedException, SystemException;

	void commit() throws RollbackException,
		HeuristicMixedException, HeuristicRollbackException, SecurityException,
		IllegalStateException, SystemException;

	void rollback() throws IllegalStateException, SecurityException,

	void setRollbackOnly() throws IllegalStateException, SystemException;

	int getStatus() throws SystemException;

	void setTransactionTimeout(int seconds) throws SystemException;