To improve the performance and scalability of your application, you can obtain your connection to the database server through a DataSource object that references a ConnectionPoolDataSource object. IBM Informix JDBC Driver provides a Connection Pool Manager as a transparent component of the ConnectionPoolDataSource object. The Connection Pool Manager keeps a closed connection in a pool instead of returning the connection to the database server as closed. Whenever a user requests a new connection, the Connection Pool Manager gets the connection from the pool, avoiding the overhead of having the server close and re-open the connection.
Using the ConnectionPoolDataSource object can significantly improve performance in cases where your application receives frequent, periodic connection requests.
For complete information about how and why to use a DataSource or ConnectionPoolDataSource object, see the JDBC 3.0 API provided by Sun Microsystems, available from the following Web site: http://java.sun.com.
The following sections discuss how to use connection pooling with IBM Informix JDBC Driver:
In the following steps:
cpds.setIfxCPMInitPoolSize(15); cpds.setIfxCPMMinPoolSize(2); cpds.setIfxCPMMaxPoolSize(20); cpds.setIfxCPMServiceInterval(30);
Context ctx = new InitialContext(); ctx.bind("myCPDS",cpds);
ds.setDataSourceName("myCPDS",ds);
Context ctx = new InitialContext(); ctx.bind("DS_Pool",ds);
During the deployment phase, you or your database administrator can control how connection pooling works in your applications by setting values for any of these Connection Pool Manager properties:
Set this property if your application will need many connections when the ConnectionPoolDataSource object is first instantiated.
To obtain the value, call getIfxCPMInitPoolSize().
To set the value, call setIfxCPMInitPoolSize (int init).
The value -1 specifies an unlimited number. The default is -1.
To obtain the value, call getIfxCPMMaxConnections().
To set the value, call setIfxCPMMaxConnections(int limit).
To obtain the value, call getIfxCPMMinPoolSize().
To set the value, call setIfxCPMMinPoolSize(int min).
To obtain the value, call getIfxCPMMaxPoolSize().
To set the value, call setIfxCPMMaxPoolSize(int max).
The default is -1, which means that the free connections are retained until the client terminates.
To obtain the value, call getIfxCPMAgeLimit().
To set the value, call setIfxCPMAgeLimit(long limit).
Use this setting to reduce resources held in the pool when there are expected periods in which no connection requests will be made. A value of 0 indicates that no additional time is given to a connection in the minimum pool: the connection is released to the server whenever it exceeds IFMX_CPM_AGELIMIT.
The default is -1, which means that a minimum number of free connections is retained until the client terminates.
To obtain the value, call getIfxCPMMinAgeLimit().
To set the value, call setIfxCPMAgeMinLimit(long limit).
Pool service activity includes adding free connections (if the number of free connections falls below the minimum value) and removing free connections. The default is 50.
To obtain the value, call getIfxCPMServiceInterval().
To set the value, call setIfxCPMServiceInterval (long interval).
Set this property if your application relies on High-Availability Data Replication with connection pooling. The default is false.
To obtain the value, call getIfxCPMSwitchHDRPool().
To set the value, call setIfxCPMSwitchHDRPool(boolean flag).
A demonstration program is available in the connection-pool directory within the demo directory where your JDBC driver is installed. For connection pooling with HDR, a demonstration program is available in the hdr directory within the demo directory. For details about the files, see Appendix A. Sample Code Files.
Some of these properties overlap Sun JDBC 3.0 properties. The following table lists the Sun JDBC 3.0 properties and their Informix equivalents.
The following Sun JDBC 3.0 properties are not supported:
IBM Informix JDBC Driver implementation of connection pooling provides the ability to pool connections with database servers in an HDR pair:
You do not have to change application code to take advantage of connection pooling with HDR. Set the IFMX_CPM_ENABLE_SWITCH_HDRPOOL property to true to allow switching between the two pools. When switching is allowed, the Connection Pool Manager validates and activates the appropriate connection pool.
When the primary server fails, the Connection Pool Manager activates the secondary pool. When the secondary pool is active, the Connection Pool Manager validates the state of the pool to check if the primary server is running. If the primary server is running, the Connection Pool Manager switches new connections to the primary server and sets the active pool to the primary pool.
If IFMX_CPM_ENABLE_SWITCH_HDRPOOL is set to false, you can force switching to the other connection pool by calling the activateHDRPool_Primary() or activateHDRPool_Secondary() methods:
public void activateHDRPool_Primary(void) throws SQLException public void activateHDRPool_Secondary(void) throws SQLException
The activateHDRPool_Primary() method switches the primary connection pool to be the active connection pool. The activateHDRPool_Secondary() method switches the secondary connection pool to be the active pool.
You can use the isReadOnly(), isHDREnabled(), and getHDRtype() methods with connection pooling (see Checking for Read-Only Status).
A demonstration program is available in the hdr directory within the demo directory where IBM Informix JDBC Driver is installed. For details about the files, see Appendix A. Sample Code Files.
You can alter connections from their original, default properties by setting database properties, such as AUTOCOMMIT and TRANSACTION ISOLATION. When a connection is closed, these properties revert to their default values. However, a pooled connection does not automatically revert to default properties when it is returned to the pool.
In IBM Informix JDBC Driver, you can call the scrubConnection() method to:
This now enables the application server to cache the statements, and it can be used across applications and sessions to provide better performance for end-user applications.
The signature of the scrubConnection() method is:
public void scrubConnection() throws SQLException
The following example demonstrates how to call scrubConnection():
try { IfmxConnection conn = (IfmxConnection)myConn; conn.scrubConnection(); } catch (SQLException e) { e.printStackTrace(); }
The following method verifies whether a call to scrubConnection() has released all statements:
public boolean scrubConnectionReleasesAllStatements()
The following table contrasts different implementations of the connection.close() and scrubConnection() methods when they are in connection pool setup or not.
Connection Pooling Status | Behavior with connection.close() Method | Behavior with scrubconnection() Method |
---|---|---|
Non-connection pool setup | Closes database connection, all associated statement objects, and their result sets Connection is no longer valid. | Returns connection to default state, keeps opened statements, but closes result sets Connection is still valid. Releases resources associated with result sets only. |
Connection Pool with Informix Implementation | Closes connection to the database and reopens it to close any statements associated with the connection object and reset the connection to its original state Connection object is then returned to the connection pool and is available when requested by a new application connection. | Returns a connection to the default state and keeps all open statements, but closes all result sets. Calling this method is not recommended here. |
Connection Pool with AppServer Implementation | Defined by user's connection pooling implementation | Returns connection to default state and retains opened statements, but closes result sets |