237 lines
14 KiB
HTML
237 lines
14 KiB
HTML
<!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 life cycle</title>
|
|
</head>
|
|
|
|
<BODY>
|
|
<!-- Java sync-link -->
|
|
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
|
|
|
|
<h6><a name="datalife"></a>Connection life cycle</h6>
|
|
|
|
<p>A ManagedConnection object is always in one of three states: DoesNotExist, InFreePool, or InUse.</p>
|
|
|
|
<p>Before a connection is created, it must be in the DoesNotExist state. After a connection is created, it can be in either the InUse or the InFreePool state, depending on whether it is allocated to an application.</p>
|
|
|
|
<p>Between these three states are transitions. These transitions are controlled by guarding conditions. A guarding condition is one in which true indicates when you can take the transition into another legal state. For example, you can make the transition from the InFreePool to InUse state only if:</p>
|
|
|
|
<ul>
|
|
<li>The application has called the data source or connection factory getConnection() method.</li>
|
|
<li>A free connection is available in the pool with matching properties (freeConnectionAvailable).</li>
|
|
<li>One of these conditions is true:
|
|
<ul>
|
|
<li>The getConnection request is on behalf of a resource reference that is marked unshareable.</li>
|
|
<li>The getConnection request is on behalf of a resource reference that is marked shareable but no shareable connection in use has the same properties.</li>
|
|
</ul></li>
|
|
</ul>
|
|
|
|
<p>This transition description follows:</p>
|
|
|
|
<pre> InFreePool > InUse:
|
|
getConnection AND
|
|
freeConnectionAvailable AND
|
|
NOT(shareableConnectionAvailable)</pre>
|
|
|
|
<p>Here is a list of guarding conditions and descriptions.</p>
|
|
|
|
<table border="1" cellpadding="3">
|
|
<tr align="left">
|
|
<th>Condition</th>
|
|
<th>Description</th>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>ageTimeoutExpired</td>
|
|
<td>Connection is older then its ageTimeout value.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>close</td>
|
|
<td>Application calls close method on the Connection object.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>fatalErrorNotification</td>
|
|
<td>A connection has just experienced a fatal error.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>freeConnectionAvailable</td>
|
|
<td>A connection with matching properties is available in
|
|
the free pool.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>getConnection</td>
|
|
<td>Application calls getConnection method on DataSource
|
|
or ConnectionFactory object.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>markedStale</td>
|
|
<td>Connection is marked as stale, typically in response
|
|
to a FatalErrorNotification.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>noOtherReferences</td>
|
|
<td>There is only one connection handle to the ManagedConnection,
|
|
and the Transaction Service is not holding a reference to the ManagedConnection.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>noTx</td>
|
|
<td>No transaction is in force.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>poolSizeGTMin</td>
|
|
<td>Connection pool size is greater than the minimum pool
|
|
size (minimum number of connections)</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>poolSizeLTMax</td>
|
|
<td>Pool size is less than the maximum pool size (maximum
|
|
number of connections)</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>shareableConnectionAvailable</td>
|
|
<td>The getConnection request was for a shareable connection
|
|
and one with matching properties is in use and available to share.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>TxEnds</td>
|
|
<td>The transaction has ended.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>unshareableConnectionRequest</td>
|
|
<td>The getConnection request is for an unshareable connection.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>unusedTimeoutExpired</td>
|
|
<td>Connection is in the free pool and not in use past its
|
|
unused timeout value.</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p><strong>Getting connections</strong></p>
|
|
<p>The first set of transitions covered are those in which the application requests a connection from either a data source or a connection factory. In some of these scenarios, a new connection to the database results. In others, the connection might be retrieved from the connection pool or shared with another request for a connection.</p>
|
|
|
|
<p><strong>DoesNotExist</strong></p>
|
|
|
|
<p>Every connection begins its life cycle in the DoesNotExist state. When an application server starts, the connection pool does not exist. Therefore, there are no connections. The first connection is not created until an application requests its first connection. Additional connections are created as needed, according to the guarding condition. </p>
|
|
|
|
<pre> getConnection AND
|
|
NOT(freeConnectionAvailable) AND
|
|
poolSizeLTMax AND
|
|
(NOT(shareableConnectionAvailable) OR
|
|
(unshareableConnectionRequest)</pre>
|
|
|
|
<p>This transition specifies that a Connection object is not created unless the following conditions occur:</p>
|
|
|
|
<ul>
|
|
<li>The application calls the getConnection() method on the data source or connection factory.</li>
|
|
<li>No connections are available in the free pool (NOT(freeConnectionAvailable)).</li>
|
|
<li>The pool size is less than the maximum pool size (poolSizeLTMax).</li>
|
|
<li>If the request is for a sharable connection and there is no sharable connection already in use with the same sharing properties (NOT(shareableConnectionAvailable)) OR the request is for an unshareable connection (unshareableConnectionRequest).</li>
|
|
</ul>
|
|
|
|
<p>All connections begin in the DoesNotExist state and are only created when the application requests a connection. The pool grows from 0 to the maximum number of connections as applications request new connections. The pool is not created with the minimum number of connections when the server starts.</p>
|
|
|
|
<p>If the request is for a sharable connection and a connection with the same sharing properties is already in use by the application, the connection is shared by two or more requests for a connection. In this case, a new connection is not created. For users of the JDBC API these sharing properties are most often userid/password and transaction context; for users of the Resource Adapter Common Client Interface (CCI) they are typically ConnectionSpec, Subject, and transaction context. </p>
|
|
|
|
<p><strong>InFreePool</strong></p>
|
|
|
|
<p>The transition from the InFreePool state to the InUse state is the most common transition when the application requests a connection from the pool.</p>
|
|
|
|
<pre> InFreePool > InUse:
|
|
getConnection AND
|
|
freeConnectionAvailable AND
|
|
(unshareableConnectionRequest OR
|
|
NOT(shareableConnectionAvailable)</pre>
|
|
|
|
<p>This transition states that a connection is placed in use from the free pool if:</p>
|
|
|
|
<ul>
|
|
<li>The application has issued a getConnection() call.</li>
|
|
<li>A connection is available for use in the connection pool (freeConnectionAvailable), and one of the following is true:
|
|
<ul>
|
|
<li>The request is for an unshareable connection (unsharableConnectionRequest).</li>
|
|
<li>No connection with the same sharing properties is already in use in the transaction. (NOT(sharableConnectionAvailable)).</li>
|
|
</ul></li>
|
|
</ul>
|
|
|
|
<p>Any connection request that a connection from the free pool can fulfill does not result in a new connection to the database. Therefore, if there is never more than one connection used at a time from the pool by any number of applications, the pool never grows beyond a size of one. This number can be less than the minimum number of connections specified for the pool. One way that a pool grows to the minimum number of connections is if the application has multiple concurrent requests for connections that must result in a newly created connection.</p>
|
|
|
|
<p><strong>InUse</strong></p>
|
|
|
|
<p>The idea of connection sharing is seen in the transition on the InUse state:</p>
|
|
|
|
<pre> InUse > InUse:
|
|
getConnection AND
|
|
ShareableConnectionAvailable</pre>
|
|
|
|
<p>This transition states that if an application requests a shareable connection (getConnection) with the same sharing properties as a connection that is already in use (ShareableConnectionAvailable), the existing connection is shared.</p>
|
|
|
|
<p>The same user (user name and password, or subject, depending on authentication choice) can share connections but only within the same transaction and only when all of the sharing properties match. For JDBC connections, these properties include the isolationLevel which is configurable on the resource-reference (IBM WebSphere extension) to data source default. For a resource adapter factory connection, these properties include those specified on the ConnectionSpec. Because a transaction is normally associated with a single thread, you should never share connections across threads.</p>
|
|
|
|
<p><strong>Note:</strong> It is possible to see the same connection on multiple threads at the same time, but this situation is an error state usually caused by an application programming error.</p>
|
|
|
|
|
|
<p><strong>Returning connections</strong></p>
|
|
<p>All of the transitions so far have covered getting a connection for application use. From this point, the transitions result in a connection closing and either returning to the free pool or being destroyed. Applications should explicitly close connections (note: the connection that the user gets back is really a connection handle) by calling close() on the Connection object. In most cases, this action results in the following transition:</p>
|
|
|
|
<pre> InUse > InFreePool:
|
|
(close AND
|
|
noOtherReferences AND
|
|
NoTx AND
|
|
UnshareableConnection)
|
|
OR
|
|
(ShareableConnection AND
|
|
TxEnds)</pre>
|
|
|
|
<p>Conditions that cause the transition from the InUse state are:</p>
|
|
|
|
<ul>
|
|
<li>If the application (or the container) calls close() (close) and there are no references (noOtherReferences) either by the application (application sharing) or by the transaction manager (NoTx - who holds a reference when the connection is enlisted in a transaction), the Connection object returns to the free pool.</li>
|
|
|
|
<li>If the connection was enlisted in a transaction but the transaction manager ends the transaction (txEnds), and the connection was a shareable connection (ShareableConnection), the connection closes and returns to the pool.</li>
|
|
</ul>
|
|
|
|
<p>When the application calls close() on a connection, it is returning the connection to the pool of free connections; it is not closing the connection to the data store. When the application calls close() on a currently shared connection, the connection is not returned to the free pool. Only after the application drops the last reference to the connection, and the transaction is over, is the connection returned to the pool. Applications using unshareable connections must take care to close connections in a timely manner. Failure to do so can starve out the connection pool making it impossible for any application running on the server to get a connection.</p>
|
|
|
|
<p>When the application calls close() on a connection enlisted in a transaction, the connection is not returned to the free pool. Because the transaction manager must also hold a reference to the connection object, the connection cannot return to the free pool until the transaction ends. Once a connection is enlisted in a transaction, you cannot use it in any other transaction by any other application until after the transaction is complete.</p>
|
|
|
|
<p>There is a case where an application calling close() can result in the connection to the data store closing and bypassing the connection being returned to the pool. This situation happens if one of the connections in the pool is considered stale. A connection is considered stale if you can no longer use it to contact the data store. For example, a connection is marked stale if the data store server is shut down. When a connection is marked as stale, the entire pool is cleaned out by default because it is very likely that all of the connections are stale for the same reason (or you can set your configuration to clean just the failing connection). This cleansing includes marking all of the currently InUse connections as stale so they are destroyed upon closing. The following transition states the behavior on a call to close() when the connection is marked as stale:</p>
|
|
|
|
<pre> InUse > DoesNotExist:
|
|
close AND
|
|
markedStale AND
|
|
NoTx AND
|
|
noOtherReferences</pre>
|
|
|
|
<p>This transition states that if the application calls close() on the connection and the connection is marked as stale during the pool cleansing step (markedStale), the connection object closes to the data store and is not returned to the pool.</p>
|
|
|
|
<p>Finally, you can close connections to the data store and remove them from the pool.</p>
|
|
|
|
<p>This transition states that there are three cases in which a connection is removed from the free pool and destroyed.</p>
|
|
|
|
<ul>
|
|
<li>If a fatal error notification is received from the resource adapter (or data source). A fatal error notification (FatalErrorNotification) is received from the resource adapter when something happens to the connection to make it unusable. All connections currently in the free pool are destroyed.</li>
|
|
<li>If the connection is in the free pool for longer than the unused timeout period (UnusedTimeoutExpired) and the pool size is greater than the minimum number of connections (poolSizeGTMin), the connection is removed from the free pool and destroyed. This mechanism enables the pool to shrink back to its minimum size when the demand for connections decreases.</li>
|
|
<li>If an age timeout is configured and a given connection is older than the timeout. The number of connections in the pool can shrink below the minimum size if connections are removed based on the age timeout value. This mechanism provides a way to recycle connections based on age.</li>
|
|
</ul>
|
|
|
|
</body>
|
|
</html>
|
|
|