ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzamy_5.4.0.1/50/program/datahand.htm

106 lines
10 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Connection handles</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h6><a name="datahand"></a>Connection handles</h6>
<p>A connection handle is a representation of a physical connection.</p>
<p>To use a back end resource (such as a relational database) in the WebSphere Application Server - Express you must get a connection to that resource. When you call the getConnection() method, you get a connection handle returned. The handle is not the physical connection. The physical connection is managed by the connection manager.</p>
<p>There are two significant configurations or usage patterns that affect how connection handles are used and how they behave. The first is the res-sharing-scope, which is defined by the resource-reference used to look up the DataSource or Connection Factory. This property tells the connection manager whether or not you can share this connection. <!--You define this property in the Application Assembly Tool when you assemble your application. For more information, see <a href="../admin/help/tdat_crtresref.html">Creating or changing a resource reference</a>. <img src="help.gif" width="18" height="15" align="absbottom" alt="Go to Help documentation">--></p>
<p>The second factor that affects connection handle behavior is the usage pattern. There are essentially two usage patterns. The first is called the <strong>get/use/close</strong> pattern. This usage pattern is where an application, within a single method and without calling another method that might get a connection from the same data source or connection factory:</p>
<ol>
<li>Get a connection.</li>
<li>Do the work.</li>
<li>Commit (if appropriate).</li>
<li>Close the connection.</li>
</ol>
<p>The second usage pattern is called the <strong>cached handle</strong> pattern. This is where an application performs these steps:</p>
<ol>
<li>Get a connection.</li>
<li>Begin a global transaction.</li>
<li>Do work on the connection.</li>
<li>Commit a global transaction.</li>
<li>Do work on the connection again.</li>
</ol>
<p>A cached handle is a connection handle that is held across transaction and method boundaries by an application. Cached handle support requires some additional connection handle management across these boundaries, which can impact performance. For example, in a JDBC application, Statements, PreparedStatements, and ResultSets are closed implicitly after a transaction ends, but the connection remains valid.</p>
<p><strong>Note:</strong> There is limited connection caching available for servlets. Connection handles can be cached only in single-threaded servlets. Caching of connection handles across servlet methods is limited to JDBC resources. Other non-relational resources, such as Customer Information Control System (CICS) or Information Management System (IMS), currently cannot have their connection handles cached in a servlet. When connection caching is not available, you must get, use, and close the connection handle within each method invocation.</p>
<p>You are encouraged not to cache the connection across the transaction boundary for shareable connections, the get/use/close pattern is preferred. This code segment shows the cached connection pattern:</p>
<pre> Connection conn = ds.getConnection();
ut.begin();
conn.prepareStatement(&quot;.....&quot;); // Connection runs in global transaction mode
...
ut.commit();
conn.prepareStatement(&quot;.....&quot;); // Connection still valid but runs in autoCommit(True);
...</pre>
<p><strong>Unshareable connections</strong></p>
<p>Some characteristics of connection handles retrieved with a res-sharing-scope of unshareable are described in the following topics.</p>
<p>Here are the possible benefits of unshared connections:</p>
<ul>
<li>Your application always maintains a direct link with a physical connection (managed connection).</li>
<li>The connection always has a one-to-one relationship between the connection handle and the managed connection.</li>
<li>In most cases, the connection does not close until the application closes it.</li>
<li>You can use a cached unshared connection handle across multiple transactions.</li>
<li>The connection can have a performance advantage in some cached handle situations. Because unshared connections do not have the overhead of moving connection handles off managed connections at the end of the transaction, there is less overhead in using a cached unshared connection.</li>
</ul>
<p>Here are the possible drawbacks of unshared connections:</p>
<ul>
<li><p>Inefficient use of your connection resources. For example, if within a single transaction you get more than one connection (with the same properties) using the same data source or connection factory (same resource-ref) then you use multiple physical connections when you use unshareable connections. This situation also precludes the use of a single phase transaction.</p></li>
<li><p>Wasted connections. It is important not to keep the connection handle open (that is, you have not called the close() method) any longer then it is needed. As long as you keep an unshareable connection open you tie up the physical connection, even if you currently are not using it.</p></li>
<li><p>Deadlock considerations. Depending on how your components interact with the database within a transaction, using unshared connections can lead to deadlocks in the database. For example, within a transaction, component A gets a connection to data source X and updates table 1, and then calls component B. Component B gets another connection to data source X, and updates/reads table 1 (or even worse the same row as component A). In some circumstances, depending on the particular database, its locking scheme, and the transaction isolation level, a deadlock can occur.</p>
<p>In the same scenario, but with a shared connection, a deadlock does not occur because all the work was done on the same connection. It is worth noting that when writing code which uses shared connections, it is important that the code be written in such a way that it expects other work to be done on the same connection, possibly within the same transaction. If you decide to use an unshareable connection, you must set the maximum connections property on the connection factory or data source correctly. An exception occurs if you try to exceed the maximum connections value.</p></li>
</ul>
<p><strong>Shareable connections</strong></p>
<p>Some characteristics of connection handles retrieved with a res-sharing-scope of shareable are described in the following topics.</p>
<p>Here are the possible benefits of shared connections:</p>
<ul>
<li><p>They can share a managed connection with one or more connection handles within a sharing, boundary depending upon how the handle is retrieved and which connection properties are used.</p></li>
<li><p>They can more efficiently use resources. Shareable connections are not valid outside of their sharing boundary. For this reason, when using the cached handle pattern, a connection handle is no longer associated with a managed connection when a sharing boundary (such as a transaction) ends. The managed connection is returned to the free connection pool for reuse. Connection resources are not held longer than the end of the current sharing scope.</p>
<p>If the cached handle pattern is used, then the next time the handle is used within a new sharing scope, the application server run time assures that the handle is reassociated with a managed connection appropriate for the current sharing scope and with the same properties with which the handle was originally retrieved. Remember that it is not appropriate to change properties on a shareable connection. If properties are changed, other components that share the same connection might experience unexpected behavior. Furthermore, when using cached handles, the value of the changed property might not be remembered across sharing scopes.</p></li>
</ul>
<p>Here are the possible drawbacks of shared connections:</p>
<ul>
<li><p>Sharing within a single component (such as an enterprise bean and its related Java objects) is not always supported. The current specification allows resource adapters the choice of only allowing one active connection handle at a time.</p>
<p>If a resource adapter chooses to implement this option then the following scenario results in an invalid handle exception: A component using shareable connections gets a connection and uses it. Without closing the connection, the component calls a utility class (Java object) which gets a connection (handle) to the same managed connection and uses it. Because the resource adapter only supports one active handle, the first connection handle is no longer valid. If the utility object returns without closing its handle, the first handle remains invalid and use of it causes an exception.</p>
<p><strong>Note:</strong> This exception occurs only when calling a utility object (a Java object).</p>
<p>Not all resource adapters have this limitation, it depends on their implementation. The WebSphere Relational Resource Adapter (RRA) does not have this limitation. Any DataSource used through the RRA does not have this limitation. If you encounter a resource adapter with this limitation you can work around it by serializing your access to the managed connection. If you always close your connection handle before getting another, or close your handle before calling code which gets another handle, and you always close your handle before you return from the method, you can allow two pieces of code to share the same managed connection. You just cannot use the connection for both events at the same time.</p></li>
<li><p>Trying to change the isolation level on a shareable JDBC based connection in a global transaction (those supported by the RRA) causes an exception. The correct way to get connections with different transaction isolation levels is by configuring the IBM extended resource-reference.</p></li>
<li><p>Closing connection handles for shareable connections by an application is not supported and causes errors. However, you can avoid this limitation by using the Relational Resource Adapter.</p></li>
</ul>
</body>
</html>