The connection sharing and connection association are the shared connection functionality. By using the shared connection functionality, you can use the resources effectively and improve the performance. Enable the connection association functionality as and when required.
When you manage transactions using local transactions, you must access the resource, such as the database, with one resource connection only. If you use the shared connection functionality, you can access the resources with one resource connection only without any consideration, in the user application.
Similarly in a global transaction, the operation is optimized to a single-phase commit when only one resource connection participates in a transaction; therefore, you can reduce the cost of concluding the transaction.
A physical connection indicates the connection to a resource you want to connect to. Normally the J2EE components such as servlets and Enterprise Beans are operated by a container, instead of being operated directly. For example, ManagedConnection (javax.resource.spi.ManagedConnection) for a resource adapter.
A logical connection indicates a connection that the J2EE components such as servlets and Enterprise Beans operate directly. For example, in the case of a resource adapter, javax.resource.cci.Connection or a connection independently provided by a resource adapter.
The relationship between a physical connection and logical connection is that, generally, a logical connection is generated from a physical connection. The physical connections are managed with a connection pool and the connection pool obtains and closes the physical connections.
For a connection request from the J2EE components such as servlets and Enterprise Beans, the connection pool generates and returns the logical connections from the physical connection. For a request to release connections, the connection pool closes the logical connections only, and manages the physical connections in the pool, without closing the connections.
If the connection association functionality is not enabled, the connections are shared by the Application Server-managed transactions. The connection sharing in a transaction uses the resource connections most effectively. The following figure shows the connection sharing in a transaction.
Figure 3-32 Relationship between the logical connection and physical connection (connection sharing in a transaction)
When the connection association functionality is enabled and the connections are shared outside the Application Server-managed transactions, the connection sharing is performed by the instances of the J2EE components such as servlets and Enterprise Beans. The following figure shows the connection sharing in components.
Figure 3-33 Relationship between the logical connection and physical connection (connection sharing in components)
For connection sharing, all the following conditions must be satisfied:
For details on defining connection sharing, see 3.14.9 Definitions in cosminexus.xml.
In the <res-sharing-scope> tag of the standard DD or cosminexus.xml of the servlets and Enterprise Beans, you specify whether the connections will be shared. You can specify the settings for each resource reference. The connection sharing is enabled by default. To disable connection sharing, specify Unshareable in <res-sharing-scope>.
For details on the J2EE application settings, see 3.14.9 Definitions in cosminexus.xml.
You cannot reuse java.sql.Connection between multiple transactions.
To use java.sql.Connection in a transaction, use the getConnection() method from javax.sql.DataSource and obtain the connections for each transaction.
Note that java.sql.Connection cannot be reused from inside the transaction to outside the transaction.
When you use a connection with persistence that has exceeded the transaction scope, connection sharing in a transaction is not available. In such a case, enable the connection association functionality.
The connection association switches the relationship between the logical connection and physical connection and implements resource access using one resource connection.
Figure 3-34 Relationship between the logical connection and physical connection (connection association)
Figure 3-35 Relationship between the logical connection and physical connection (when connection association and connection sharing are used together)
For connection association, all the following conditions must be satisfied:
The connection association is disabled by default.
Specify the settings for enabling connection association by customizing the J2EE server properties. For details on customizing the J2EE server operation settings, see 3.14.10 Settings in the execution environment.
The products from java.sql.Connection (example: java.sql.Statement) cannot be used exceeding the transaction scope.