WebSphere Application Server supports both unshareable and shareable connections. An unshareable connection is not shared with other components in the application. The component using this connection has full control of this connection. You can share a shareable connection with other components within the same transaction as long as each getConnection() request has the same connection properties. To enable connection sharing for data sources, the following connection properties must be the same:
To enable connection sharing for resource adapters within the same transaction, the following connection properties must be the same:
In addition, the ConnectionSpec object used to get the connection must also be the same. For more information on sharing a connection with a CMP bean, see Sharing a connection with a CMP bean.
Java Message Service (JMS) connections cannot be shared with non-JMS connections.
Access to a resource marked as unshareable means that there is a one-to-one relationship between the connection handle a component is using and the physical connection with which the handle is associated. This access implies that every call to the getConnection() method returns a connection handle solely for the requesting user. Typically, choose unshareable if you might do things to the connection that could result in unexpected behavior occurring in another application that is sharing the connection (for example, unexpectedly changing the isolation level).
Marking a resource as shareable allows for greater scalability. Instead of creating new physical connections on every getConnection() invocation, the physical connection (that is, managed connection) is shared through multiple connection handles, as long as each getConnection request has the same connection properties. However, sharing a connection means that each user must not do anything to the connection that could change its behavior and disrupt a sharing partner (for example, changing the isolation level). The user also cannot code an application that assumes sharing to take place because it is up to the run time to decide whether or not to share a particular connection.
For WebSphere Application Server, all sharing of connections is relative to the current Unit of Work (UOW) boundary. Anyone within a specific transaction,
when getting a connection from a specific connection pool, gets a handle to the same physical connection (if the sharing properties are the same).
Related concepts
Resource adapter
Connection factory
Data sources
Connection pooling
Connection handles
Isolation level and resource reference
Related tasks
Using the transaction service
Related reference
Extensions to data access APIs
Connection considerations when migrating servlets,
JavaServer Pages, or enterprise session beans
Access intent and isolation level
Access intent policies
The listing here is not an exhaustive one. The product might or might not share connections under different circumstances.
The connection returned on the second get is the same connection as that returned on the first get (if the same properties are used). Because the connection use is serial, only one connection handle to the underlying physical connection is used at a time, so true connection sharing does not take place. The term “reuse” is more accurate.
More importantly , the LocalTransactionContainment boundary enclosing both get actions is not complete; no cleanUp() method is invoked on the ManagedConnection object. Therefore the second get action inherits all of the connection properties set during the first getConnection() call.
However, the properties set on the connection during one transaction are not guaranteed to be the same when used in the next transaction. Because it is not valid to share connections outside of a sharing scope, connection handles are moved off of the physical connection with which they are currently associated when a transaction ends. That physical connection is returned to the free connection pool. Connections are cleaned before going in the free pool. The next time the handle is used, it is automatically associated with an appropriate connection. The appropriateness is based on the security login information, connection properties, and (for the JDBC API) the isolation level specified in the extended resource reference, passed in on the original request that returned the current handle. Any properties set on the connection after it was retrieved are lost.
Use caution when setting properties and sharing connections in a local transaction scope. Ensure that other components with which the connection is shared are expecting the behavior resulting from your settings.
WebSphere Application Server allows you to share a physical connection between a CMP bean, a BMP bean, and a JDBC application to reduce the resource allocation or deadlock scenarios. There are several ways to ensure that all of these entity beans and the JDBC applications are sharing the same physical connection.
When all CMP bean methods use the same access intent, they all share the same physical connection. A different access intent policy triggers the allocation of a different physical connection. For example, a CMP bean has two methods; method 1 is associated with wsPessimisticUpdate intent, whereas method 2 has wsOptimisticUpdate access intent. Method 1 and method 2 cannot share the same physical connection within a transaction. In other words, an XA data source is required to run in a global transaction.
You can experience some deadlocks from a database if both methods try to access the same table. Therefore, sharing a connection is determined by the access intents that are defined in the CMP methods.
Determine the isolation level that the access intent uses on a CMP bean method, then use the corresponding isolation level specified on the resource reference to look up a data source and a connection. For more information refer to Access intent isolation levels and update locks and Isolation level and resource reference.
There is a new exception, the SharingViolation exception,
that the resource adapter can issue whenever an operation violates sharing requirements. Possible violations include changing connection attributes,
security settings, or isolation levels, among others. When such a mutable operation is performed against a managed connection, the SharingViolation exception can occur when both of the following conditions are true:
Both the component and the J2C run time might need to detect this SharingViolation exception, depending on when and how the managed connection becomes unshareable. If the managed connection becomes unshareable because of an operation through the connection handle (for example, you change the isolation level), then the component needs to process the exception. If the managed connection becomes unshareable without being recognized by the application server (due to some component interaction with the connection handle), then the resource adapter can reject the creation of a connection