Interact with connection pools to obtain and release connections

Connection handles

A connection handle is a representation of a physical connection.

To use a backend resource (such as a relational database) in the WebSphere Application Server 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.

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-ref used to look up the DataSource or Connection Factory. This property tells the connection manager whether or not you can share this connection.


<resource-ref id="ResourceRef_1122305703058">
	<description>Titan application Data Source</description>
	<res-ref-name>jdbc/TitanDS</res-ref-name>
	<res-type>javax.sql.DataSource</res-type>
	<res-auth>Container</res-auth>
	<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
					
					

The second factor that affects connection handle behavior is the usage pattern. There are essentially two usage patterns. The first is called the get/use/close pattern. It is used within a single method and without calling another method that might get a connection from the same data source or connection factory. The second usage pattern is called the cached handle pattern.

The possible benefits of unshared connections

The possible drawbacks of unshared connections

The possible benefits of shared connections

The possible drawbacks of shared connections

Getting a connection from the DataSource

A connection is obtained from the DataSource. The connection is used as needed, and then released with a call to close() so that server knows it is available for a subsequent request.

It is very important that the close() is always called, even if there as an exception. Without the close(), server's database pool can loose connections. If you fail to close() a connection, server does not know that it is available for reuse, and cannot allocate it for another request. Eventually, server may run out of connections.

Always put a close() in a finally block, to guarantee that it is called.

The following example shows the use of a finally block that contains the close(). Because the close() is in a finally block, it will happen even if the code using the connection throws an exception:

Connection conn = null;
try {
	conn = dataSource.getConnection();
	Statement stmt = conn.createStatement();
	ResultSet rs = stmt.executeQuery(" ... ");
	...
	rs.close();
	stmt.close();
} catch (SQLException e) {
	throw new ServletException(e);
} finally {
	try {
		if (conn != null)
			conn.close();
	} catch (SQLException e) {
	}
}
					

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     Free SCWCD 1.4 Study Guide