!--a11y-->
Connection Handling in Distributed and Local
Transactions 
The enlistment of the connections in transactions depends on their transaction support. A connection may come from a connection factory with NoTransaction, LocalTransaction or XATransaction support, or it may be declared non-transactional. 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 distributed or a local transaction.
A connection is transactional – that is, it may be used in a distributed transaction if the connection factory that provided the connection is with LocalTransaction or XATransaction support and is not declared non-transactional in the deployment descriptor of the application component.
The connection cannot be used in a transaction in the following cases:
· The connection factory is with NoTransaction support
·
The factory is declared non-transactional in the
additional deployment descriptor of the application component – for
enterprise beans
ejb-j2ee-engine.xml,
and for Web components
web-j2ee-engine.xml.
The following example demonstrates how a resource is defined non-transactional:

<resource-ref>
<res-ref-name>jdbc/CAR_RENTAL_POOL<res-ref-name>
<res-link>CAR_RENTAL_POOL</res-link>
<non-transactional/>
</resource-ref>
You may invoke the getConnection() method of the connection factory within the scope of a global transaction. If the connection is declared Shareable, its transaction support is irrelevant and you obtain a connection handle. However, if the connection is Unshareable, its transaction support defines the behavior of the system. For a connection with LocalTransaction support the system checks if there is another resource with LocalTransaction support already enlisted in the distributed transaction. If there is such resource, you will get an exception because the J2EE standard allows a single LocalTransaction resource to be enlisted in a global transaction. If the connection is with XATransaction support, you can obtain a connection handle.
SAP recommends that you close the connections explicitly in the application code. Typically, you can use the connection in a try clause and close it in the finallyblock.

try{ ... Connection con = cf.getConnection(); // Do work ... }finally { con.close(); } |
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 only if it is a database connection. If the connection is provided by a generic resource adapter, the Connector Container Service does not close it and it cannot be returned to the connection pool. Eventually, this results in a resource leak.
