Because an application uses connection pooling by referencing the JNDI name of a registered PooledConnectionDataSource object, code changes are not required for an application to use connection pooling.
The following example for the Oracle driver shows Java code that looks up and uses the JNDI-registered PooledConnectionDataSource object created in "Creating the connection pool."
//********************************************************************
// Test program to look up and use a JNDI-registered data source.
//
// To run the program, specify the JNDI lookup name for the
// command-line argument, for example:
//
// java TestDataSourceApp <jdbc/ConnectOracle>
//********************************************************************
import javax.sql.*;
import java.sql.*;
import javax.naming.*;
import java.util.Hashtable;
public class TestDataSourceApp
{ public static void main(String argv[])
{
String strJNDILookupName = "";
// Get the JNDI lookup name for a data source
int nArgv = argv.length;
if (nArgv != 1) {
// User does not specify a JNDI lookup name for a data source,
System.out.println(
"Please specify a JNDI name for your data source");
System.exit(0);
else {
strJNDILookupName = argv[0];
}
DataSource ds = null;
Connection con = null;
Context ctx = null;
Hashtable env = null;
long nStartTime, nStopTime, nElapsedTime;
// Set up environment for creating InitialContext object
env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, "file:c:\\JDBCDataSource");
try {
// Retrieve the DataSource object that is bound to the logical
// lookup JNDI name
ctx = new InitialContext(env);
ds = (DataSource) ctx.lookup(strJNDILookupName);
catch (NamingException eName) {
System.out.println("Error looking up " +
strJNDILookupName + ": " +eName);
System.exit(0);
}
int numOfTest = 4;
int [] nCount = {100, 100, 1000, 3000};
for (int i = 0; i < numOfTest; i ++) {
// Log the start time
nStartTime = System.currentTimeMillis();
for (int j = 1; j <= nCount[i]; j++) {
// Get Database Connection
try {
con = ds.getConnection("scott", "tiger");
// Do something with the connection
// ...
// Close Database Connection
if (con != null) con.close();
} catch (SQLException eCon) {
System.out.println("Error getting a connection: " + eCon);
System.exit(0);
} // try getConnection
} // for j loop
// Log the end time
nStopTime = System.currentTimeMillis();
// Compute elapsed time
nElapsedTime = nStopTime - nStartTime;
System.out.println("Test number " + i + ": looping " +
nCount[i] + " times");
System.out.println("Elapsed Time: " + nElapsedTime + "\n");
} // for i loop
// All done
System.exit(0);
// Main
} // TestDataSourceApp
Note: To use non-pooled connections, specify the JNDI name of a registered driver DataSource object as the command-line argument when you run the preceding application. For example, the following command specifies the driver DataSource object created in "Creating a driver DataSource object": java TestDataSourceApp jdbc/ConnectOracle.