skip to main content
JDBC Connection Pool Manager : Connecting to a Data Source
 

Connecting to a Data Source

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 shows Java code that looks up and uses the JNDI-registered data source for connections. You specify the JNDI lookup name for the data source you created (as described in Creating a Data Source Using the DataDirect Connection Pool Manager).
//***********************************************************
//
// 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
//
//********************************************************************
import javax.sql.*;
import java.sql.*;
import javax.naming.*;
import java.util.Hashtable;
 
public class TestDataSourceApp
{
public static void main(String argv[])
{
String str JNDILookupName = "jdbc/SparkyOracle";
 
// Hard-code the JNDI entry, the application does not need to change
 
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 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: The JDBC DataSource object class implements the DataSource interface for non-pooling in addition to ConnectionPoolDataSource for pooling. To use non-pooled connections, modify the example in Creating a DataDirect OpenAccess SDK Data Source Object so that it registers the OpenAccess SDK Data Source using the JNDI entry
<jdbc/SparkyOracle>
You can then run the TestDataSourceApp without any modification:
java TestDataSourceApp