3 Advanced Features : Using Connection Pooling

Using Connection Pooling
Typically, creating a connection is the most performance-expensive operation that an application performs. Connection pooling allows you to reuse connections rather than create a new one every time a driver needs to establish a connection to the database. Connection pooling manages connection sharing across different user requests to maintain performance and reduce the number of new connections that must be created. For example, compare the following transaction sequences.
Example A: Without Connection Pooling
1
2
3
4
5
Example B: With Connection Pooling
1
2
3
4
5
6
NOTE: The application calls the close() method, but the connection remains open and the pool is notified of the close request.
Understanding Connection Pooling
A connection pool implementation, such as the DataDirect Connection Pool Manager, creates database connections, referred to as PooledConnections, using the getPooledConnection() method of the ConnectionPoolDataSource interface. Then, the Pool Manager registers itself as a listener to the PooledConnection. When a client application requests a connection, the Pool Manager assigns an available connection. If a connection is unavailable, the Pool Manager establishes a new connection and assigns it to that application.
When the application closes the connection, the Pool Manager is notified by the driver by the ConnectionEventListener interface that the connection is free and available for reuse. The Pool Manager is also notified by the ConnectionEventListener interface if the database connection is corrupted so that the Pool Manager can remove that connection from the pool.
Connection pool implementations, such as the DataDirect Connection Pool Manager, use objects that implement the javax.sql.ConnectionPoolDataSource interface to create the connections managed in the pool. All DataDirect Connect Series for JDBC DataSource objects implement the ConnectionPoolDataSource interface.
Once a data source has been created and registered with JNDI, it can be used by your JDBC application as shown in the following example, typically through a third-party connection pool tool:
Context ctx = new InitialContext();
ConnectionPoolDataSource ds = (ConnectionPoolDataSource)ctx.lookup("EmployeeDB");
Connection conn = ds.getConnection("scott", "tiger");
In this example, first, the JNDI environment is initialized. Next, the initial naming context is used to find the logical name of the JDBC data source (EmployeeDB). The Context.lookup method returns a reference to a Java object, which is narrowed to a javax.sql.ConnectionPoolDataSource object. Next, the ConnectionPoolDataSource.getPooledConnection() method is called to establish a connection with the underlying database. Finally, the application obtains a connection from the ConnectionPoolDataSource.
The DataDirect Connection Pool Manager is shipped with the product. Refer to Chapter 9 “Connection Pool Manager” in the DataDirect Connect Series for JDBC Reference for more information about managing connection pools using the DataDirect Connection Pool Manager.
Using Reauthentication
Typically, you can configure a connection pool to provide scalability for connections. In addition, to help minimize the number of connections required in a connection pool, you can switch the user associated with a connection to another user, a process known as reauthentication.
For example, suppose you are using Kerberos authentication to authenticate users using their operating system user name and password. To reduce the number of connections that must be created and managed, you can use reauthentication to switch the user associated with a connection to multiple users. For example, suppose your connection pool contains a connection, Conn, which was established by the user ALLUSERS. That connection can service multiple users (User A, B, and C) by switching the user associated with the connection Conn to User A, B, and C.
Not all databases support reauthentication. For databases that do, the user performing the switch must have been granted specific database permissions.
Table 3-8 shows the support for reauthentication and lists the required database permissions. Refer to your database documentation for information about granting permissions.
Oracle8i R2 and higher 1

1
Oracle refers to reauthentication as proxy authentication.

2
Microsoft SQL Server refers to reauthentication as impersonation.

NOTE: Before performing reauthentication, applications must ensure that any statements or result sets created as one user are closed before switching the connection to another user.
To perform reauthentication on a connection explicitly, call the setCurrentUser() method in the ExtConnection interface located in the com.ddtek.jdbc.extensions package. For example, the following code switches the user on the connection from the user that created the connection (TEST) to a new user (SMITH). In addition, it sets options that switch the current schema and current database path to the schema and database path of the new user.
import com.ddtek.jdbc.extensions.*
// Get Database Connection
Connection con = DriverManager.getConnection(
   "jdbc:datadirect:db2://server1:50000;DatabaseName=jdbc", "TEST", "secret");
ExtConnection extCon = (ExtConnection)con
Properties props = new Properties();
 
props.put("CURRENT_SCHEMA", "SCHEMA");
props.put("CURRENT_PATH", "PATH");
ExtCon.setCurrentUser("SMITH", props);
...
See the appropriate driver chapters for information on supported options for each driver.
If you are using the DataDirect Connection Pool Manager for connection pooling, you can enable reauthentication in the Pool Manager. Refer to Chapter 9 “Connection Pool Manager” in the DataDirect Connect Series for JDBC Reference for details.