This is an example of testing one DataSource that uses connection pooling only and the other DataSource that uses statement and connection pooling.
import java.sql.*;
import javax.naming.*;
import java.util.*;
import javax.sql.*;
import com.ibm.db2.jdbc.app.UDBDataSource;
import com.ibm.db2.jdbc.app.UDBConnectionPoolDataSource;
public class StatementPoolingTest
{
public static void main(java.lang.String[] args)
throws Exception
{
Context ctx = new InitialContext();
System.out.println("deploying statement pooling data source");
deployStatementPoolDataSource();
// Do the work with connection pooling only.
DataSource ds = (DataSource) ctx.lookup("PoolingDataSource");
System.out.println("\nStart timing the connection pooling only version...");
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
Connection c1 = ds.getConnection();
PreparedStatement ps = c1.prepareStatement("select * from qsys2.sysprocs");
ResultSet rs = ps.executeQuery();
c1.close();
}
long endTime = System.currentTimeMillis();
System.out.println("Time spent: " + (endTime - startTime));
// Do the work with statement pooling added.
ds = (DataSource) ctx.lookup("StatementPoolingDataSource");
System.out.println("\nStart timing the statement pooling version...");
startTime = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
Connection c1 = ds.getConnection();
PreparedStatement ps = c1.prepareStatement("select * from qsys2.sysprocs");
ResultSet rs = ps.executeQuery();
c1.close();
}
endTime = System.currentTimeMillis();
System.out.println("Time spent: " + (endTime - startTime));
}
private static void deployStatementPoolDataSource()
throws Exception
{
// Create a ConnectionPoolDataSource implementation
UDBConnectionPoolDataSource cpds = new UDBConnectionPoolDataSource();
cpds.setDescription("Connection Pooling DataSource object with Statement pooling");
cpds.setMaxStatements(10);
// Establish a JNDI context and bind the connection pool data source
Context ctx = new InitialContext();
ctx.rebind("StatementSupport", cpds);
// Create a standard datasource that references it.
UDBDataSource ds = new UDBDataSource();
ds.setDescription("DataSource supporting statement pooling");
ds.setDataSourceName("StatementSupport");
ctx.rebind("StatementPoolingDataSource", ds);
}
}