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.
// java TestDataSourceApp <jdbc/SparkyOracle>
public static void main(String argv[])
String strJNDILookupName = "";
// Get the JNDI lookup name for a data source
// User does not specify a JNDI lookup name for a data source,
"Please specify a JNDI name for your data source");
strJNDILookupName = argv[0];
long nStartTime, nStopTime, nElapsedTime;
// Set up environment for creating InitialContext object
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, "file:c:\\JDBCDataSource");
// Retrieve the DataSource object that is bound to the logical
ctx = new InitialContext(env);
ds = (DataSource) ctx.lookup(strJNDILookupName);
catch (NamingException eName) {
System.out.println("Error looking up " +
strJNDILookupName + ": " +eName);
int [] nCount = {100, 100, 1000, 3000};
for (int i = 0; i < numOfTest; i ++) {
nStartTime = System.currentTimeMillis();
for (int j = 1; j <= nCount[i]; j++) {
// Get Database Connection
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);
nStopTime = System.currentTimeMillis();
nElapsedTime = nStopTime - nStartTime;
System.out.println("Test number " + i + ": looping " +
System.out.println("Elapsed Time: " + nElapsedTime + "\n");
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”: