Show TOC Start of Content Area

Background documentation Connection Handling in JTA and Local Transactions  Locate the document in its SAP Library structure

Use

The enlistment of connections in transactions depends on their transaction support. A connection may come from a connection factory with one of the following transaction support levels:

      NoTransaction – the resource does not support transactions

      LocalTransaction – the resource supports local transactions only

      XATransaction – the resource supports both local and distributed transactions

The connection may also come from a factory for which you have declared a nontransactional resource reference.

In addition, the events that occur when either getConnection() is invoked on the connection factory, or connection.close()is called, depend on whether the connection is used in a JTA transaction or a local transaction.

Features

Transactional and Nontransactional Resources

A connection is transactional – that is, it may be enlisted in a distributed transaction if the connection factory that provided the connection is with LocalTransaction or XATransaction support and is not declared nontransactional in the deployment descriptor of the application component.

The connection is not enlisted in a transaction in the following cases:

      The connection factory is with NoTransaction support

      You have declared a nontransactional resource reference for the factory in the SAP-specific deployment descriptor of the application component. The deployment descriptors are ejb-j2ee-engine.xml (for enterprise beans) and web-j2ee-engine.xml (for Web components).

The following example demonstrates how a resource is defined nontransactional:

Example

<resource-ref>

  <res-ref-name>jdbc/CAR_RENTAL_POOL<res-ref-name>

  <res-link>CAR_RENTAL_POOL</res-link>

  <non-transactional/>

</resource-ref>

Example

For example, you may use a nontransactional resource if you want to access both an SAP R/3 system and MaxDB in a single transaction. Since the connections to both back-end systems are not XA-enabled, you must declare the resource reference to one of them as nontransactional to avoid having more than one resource with LocalTransaction support enlisted in the transaction.

Using Connections in JTA Transactions

The transaction support of a connection has important implications when using the resource in a JTA transaction. The following table describes the scenarios that are possible if you invoke the getConnection() method of the connection factory within the scope of a JTA transaction, and the expected system behavior in each scenario:

LocalTransaction

The Java EE standard allows a single LocalTransaction resource to be enlisted in a JTA transaction. Therefore, if another resource with LocalTransaction support has already been enlisted in the transaction, you get an exception. Otherwise, you get a connection.

XATransaction

You get a connection because there are no limitations to the number of XATransaction resources that may be enlisted in a JTA transaction.

Recommendation

We recommend that you use connection sharing as this makes transaction and resource management easier.

More information: Sharing Connections

Closing a Connection

You must close the connections explicitly in the application code. Typically, you can use the connection in a try clause and close it in the finally block.

Example

Connection con = cf.getConnection();

try{

   ...

   // Do work

   ...

}finally {

   con.close();

}

Typically, if you invoke connection.close() on a connection used in a local transaction before its end, the transaction is rolled back and the connection is returned to the pool.

End of Content Area