9 Connection Pool Manager : Creating a Connection Pool

Creating a Connection Pool
1
Create and register with JNDI a DataDirect Connect Series for JDBC driver DataSource object. Once created, this DataSource object can be used by a connection pool (PooledConnectionDataSource object created in Step 2) to create connections for one or multiple connection pools.
2
To create a connection pool, you must create and register with JNDI a PooledConnectionDataSource object. A PooledConnectionDataSource creates and manages one or multiple connection pools. The PooledConnectionDataSource uses the driver DataSource object created in Step 1 to create the connections for the connection pool.
Creating a Driver DataSource Object
The following Java code example creates a DataDirect Connect Series for JDBC driver DataSource object and registers it with a JNDI naming service.
The DataSource class is provided by DataDirect Connect Series for JDBC and is database-dependent. In this example we use Oracle, so the DataSource class is OracleDataSource. Refer to the appropriate driver chapter in the DataDirect Connect Series for JDBC User’s Guide for the name of the DataSource class for your driver.
NOTE: The DataSource class implements the ConnectionPoolDataSource interface for pooling in addition to the DataSource interface for non-pooling.
//************************************************************************
// This code creates a DataDirect Connect Series for JDBC data source and
// registers it to a JNDI naming service. This JDBC data source uses the
// DataSource implementation provided by DataDirect Connect Series
// for JDBC Drivers.
//
// This data source registers its name as <jdbc/ConnectSparkyOracle>.
//
// NOTE: To connect using a data source, the driver needs to access a JNDI data // store to persist the data source information. To download the JNDI File
// System Service Provider, go to:
//
// http://www.oracle.com/technetwork/java/javasebusiness/downloads/// java-archive-downloads-java-plat-419418.html#7110-jndi-1.2.1-oth-JPR
//
// Make sure that the fscontext.jar and providerutil.jar files from the
// download are on your classpath.
//************************************************************************
 
// From DataDirect Connect Series for JDBC:
import com.ddtek.jdbcx.oracle.OracleDataSource;
 
import javax.sql.*;
import java.sql.*;
import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;
 
public class OracleDataSourceRegisterJNDI
{
   public static void main(String argv[])
   {
      try {
      // Set up data source reference data for naming context:
      // ----------------------------------------------------
      // Create a class instance that implements the interface
      // ConnectionPoolDataSource
      OracleDataSource ds = new OracleDataSource();
 
      ds.setDescription("Oracle on Sparky - Oracle Data Source");
      ds.setServerName("sparky");
      ds.setPortNumber(1521);
      ds.setUser("scott");
      ds.setPassword("test");
 
      // Set up environment for creating initial context
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY,
         "com.sun.jndi.fscontext.RefFSContextFactory");
      env.put(Context.PROVIDER_URL, "file:c:\\JDBCDataSource");
      Context ctx = new InitialContext(env);
 
      // Register the data source to JNDI naming service
      ctx.bind("jdbc/ConnectSparkyOracle", ds);
 
      } catch (Exception e) {
      System.out.println(e);
      return;
      }
   } // Main
   // class OracleDataSourceRegisterJNDI
Creating the Connection Pool
To create a connection pool, you must create and register with JNDI a PooledConnectionDataSource object. The following Java code creates a PooledConnectionDataSource object and registers it with a JNDI naming service.
To specify the driver DataSource object to be used by the connection pool to create pooled connections, set the parameter of the DataSourceName method to the JNDI name of a registered driver DataSource object. For example, the following code sets the parameter of the DataSourceName method to the JNDI name of the driver DataSource object created in “Creating a Driver DataSource Object”.
The PooledConnectionDataSource class is provided by the DataDirect com.ddtek.pool package. See “PooledConnectionDataSource” for a description of the methods supported by the PooledConnectionDataSource class.
//************************************************************************
// This code creates a data source and registers it to a JNDI naming service.
// This data source uses the PooledConnectionDataSource
// implementation provided by the DataDirect com.ddtek.pool package.
//
// This data source refers to a registered
// DataDirect Connect Series for JDBC driver DataSource object.
//
// This data source registers its name as <jdbc/SparkyOracle>.
//
// NOTE: To connect using a data source, the driver needs to access a JNDI data // store to persist the data source information. To download the JNDI File
// System Service Provider, go to:
//
// http://www.oracle.com/technetwork/java/javasebusiness/downloads/// java-archive-downloads-java-plat-419418.html#7110-jndi-1.2.1-oth-JPR
//
// Make sure that the fscontext.jar and providerutil.jar files from the
// download are on your classpath.
//************************************************************************
 
// From the DataDirect connection pooling package:
import com.ddtek.pool.PooledConnectionDataSource;
 
import javax.sql.*;
import java.sql.*;
import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;
 
public class PoolMgrDataSourceRegisterJNDI
{
   public static void main(String argv[])
   {
      try {
         // Set up data source reference data for naming context:
         // ----------------------------------------------------
         // Create a pooling manager's class instance that implements
         // the interface DataSource
         PooledConnectionDataSource ds = new PooledConnectionDataSource();
 
         ds.setDescription("Sparky Oracle - Oracle Data Source");
 
         // Specify a registered driver DataSource object to be used
         // by this data source to create pooled connections
         ds.setDataSourceName("jdbc/ConnectSparkyOracle");
 
         // The pool manager will be initiated with 5 physical connections
         ds.setInitialPoolSize(5);
 
         // The pool maintenance thread will make sure that there are 5
         // physical connections available
         ds.setMinPoolSize(5);
 
         // The pool maintenance thread will check that there are no more
         // than 10 physical connections available
         ds.setMaxPoolSize(10);
 
         // The pool maintenance thread will wake up and check the pool
         // every 20 seconds
         ds.setPropertyCycle(20);
 
         // The pool maintenance thread will remove physical connections
         // that are inactive for more than 300 seconds
         ds.setMaxIdleTime(300);
 
         // Set tracing off because we choose not to see an output listing
         // of activities on a connection
         ds.setTracing(false);
 
         // Set up environment for creating initial context
         Hashtable env = new Hashtable();
         env.put(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.fscontext.RefFSContextFactory");
         env.put(Context.PROVIDER_URL, "file:c:\\JDBCDataSource");
         Context ctx = new InitialContext(env);
 
         // Register this data source to the JNDI naming service
         ctx.bind("jdbc/SparkyOracle", ds);
 
         catch (Exception e) {
         System.out.println(e);
         return;
      }
 
   }
}