DB2ConnectionPool class

The com.ibm.db2.jcc.dbpool.DB2ConnectionPool class provides connection pooling support.

In addition to providing connection pooling support, a DB2ConnectionPool instance supports all methods and properties of the DB2ConnectionPoolDataSource class.

The DB2ConnectionPool class is supported in IBM® Data Server Driver for JDBC and SQLJ version 4.29.24 or later.

DB2ConnectionPool constructors

Formats:
public DB2ConnectionPool()
  throws java.sql.SQLException
public DB2ConnectionPool(String url, Properties properties)
  throws java.sql.SQLException
DB2ConnectionPool(Db2SimpleDataSource ds)
  throws java.sql.SQLException
DB2ConnectionPool(DB2ConnectionPoolDataSource cpds)
  throws java.sql.SQLException
Example: Default constructor
DB2ConnectionPool pool = new DB2ConnectionPool();
Example: Constructor with a JDBC URL as an argument
String URL = "jdbc:db2://server:port/databasename:traceLevel=-1";
Properties props = new Properties();
props.put("user", "user");
props.put("password", "pwd");
D2ConnectionPool pool = new DB2ConnectionPool(URL, props);
Example: Constructor with a DB2SimpleDataSource instance as an argument
DB2SimpleDataSource ds = new DB2SimpleDataSource(); 
ds.setServerName("serverName");
ds.setPortNumber(3950);
ds.setDataBaseName("databasename");
ds.setDriverType(2);
DB2ConnectionPool pool = new DB2ConnectionPool(ds);
Example: Constructor with a DB2ConnectionPoolDataSource instance as an argument
DB2ConnectionPoolDataSource cpds=new DB2ConnectionPoolDataSource(); 
cpds.setServerName("serverName");
cpds.setPortNumber(3950);
cpds.setDataBaseName("databasename");
cpds.setDriverType(2);
DB2ConnectionPool pool = new DB2ConnectionPool(cpds);

DB2ConnectionPool properties

These properties have a setXXX method to set the value of the property and a getXXX method to retrieve the value.

A setXXX method has this form:
void setProperty-name(data-type property-value)
A getXXX method has this form:
data-type getProperty-name()
Property-name is the unqualified property name, with the first character capitalized.

The following table lists the IBM Data Server Driver for JDBC and SQLJ properties and their data types.

Table 1. DB2ConnectionPool properties and their data types
Property name Data type
acquireIncrement int
acquireRetryAttempts int
acquireRetryDelay int
autoCommit boolean
connectionTimeout int
databaseName String
driverType int
initialPoolSize int
loginTimeout int
logWriter java.io.PrintWriter
maxIdleTime int
maxPoolSize int
password String
poolLoggingFrequency int
portNumber int
preferredTestQuery int
serverName int
testConnectionOnCheckin int
testConnectionOnCheckout int
traceFile String
traceLevel int
user int

For detailed descriptions of the properties, see Common IBM Data Server Driver for JDBC and SQLJ properties for Db2 and Db2 for z/OS servers.

DB2ConnectionPool methods

getConnection
Format:
public java.sql.Connection getConnection()
  throws java.sql.SQLException

Establishes the initial connection in a connection pooling environment.

getDataSource
Formats:
public DB2ConnectionPoolDataSource getDataSource()

Returns a DB2ConnectionPoolDataSource object on which additional properties can be set.

getPoolStats
Format:
public String getPoolStats()

Returns connection pooling statistics.

testConnection
Format:
public void testConnection()
  throws java.sql.SQLException

Tests the pooled connection.

How properties can be set when you construct DB2ConnectionPool objects

The way that you set properties on a DB2ConnectionPool object depends on the constructor signature constructor.

For each DB2ConnectionPool object, there is an underlying DB2ConnectionPoolDataSource object. If you use a DB2ConnectionPool constructor format that does not support a property directly, you can set the property on the underlying DB2ConnectionPoolDataSource object.

The following table lists the constructor signatures and how properties can be set when you use each type of constructor.

Constructor signature How properties can be set
public DB2ConnectionPool()
These properties can be set on the DB2ConnectionPool object:
  • user
  • password
  • serverName
  • portNumber
  • databaseName
  • traceFile
  • traceLevel
  • loginTimeout
  • connectionTimeout

Any other properties can be set on the underlying DB2ConnectionPoolDataSource object.

public DB2ConnectionPool(String url, Properties properties) These properties can be set on the url or on the Properties object:
  • user
  • password
  • serverName
  • portNumber
  • databaseName

Any other properties can be set on the underlying DB2ConnectionPoolDataSource object.

DB2ConnectionPool(DB2SimpleDataSource ds) These properties can be set on the DB2SimpleDataSource object:
  • user
  • password
  • serverName
  • portNumber
  • databaseName
  • traceFile
  • traceLevel
  • loginTimeout
  • connectionTimeout

Any other properties can be set on the underlying DB2ConnectionPoolDataSource object.

DB2ConnectionPool(DB2ConnectionPoolDataSource cpds) All properties can be set on the DB2ConnectionPoolDataSource object.

Example

Suppose that you are constructing a DB2ConnectionPool object using the constructor with the DB2SimpleDataSource parameter. You need to set the following properties on the DB2ConnectionPool object:
  • serverName
  • portNumber
  • databaseName
  • driverType
  • maxRowsetSize
You can set serverName, portNumber, and databaseName on the DB2SimpleDataSource object that is the parameter of the DB2ConnectionPool constructor. You cannot set driverType and maxRowsetSize on the DB2SimpleDataSource object, so you need to set them on the DB2ConnectionPoolDataSource object that underlies the DB2ConnectionPool object. Use code like this to set the properties.
DB2SimpleDataSource ds = new DB2SimpleDataSource();
                                        // Construct a DB2SimpleDataSource object 
ds.setServerName("myserver");           // Set the serverName, portNumber, and 
ds.setPortNumber(3950);                 // database properties on the 
ds.setDataBaseName("mydb");             // DB2SimpleDataSource object

DB2ConnectionPool pool = new DB2ConnectionPool(ds);
                                        // Construct a DB2ConnectionPool object
                                        // using the DB2SimpleDataSource object
DB2ConnectionPoolDataSource pooledDataSource = 
 pool.getDataSource();                  // Retrieve the DB2ConnectionPoolDataSource
                                        // object that underlies the 
                                        // DB2ConnectionPool object
pooledDataSource.setDriverType(2);      // Set driverType and maxRowsetSize on
pooledDataSource.setMaxRowsetSize(450); // the DB2ConnectionPoolDataSource object