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

Try DataDirect Cloud Now

Connecting to a JDBC Data Source Using a Connection Pool

Once a connection pool has been created and registered with JNDI, it can be used by your JDBC application when it creates the connection to the JDBC data source as shown in the following code snippet, typically through a third-party connection pool tool:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/PoolCloudSparky");
Connection conn = ds.getConnection("DDusername", "DDpassword");
In this example, first, the JNDI environment is initialized. Next, the initial naming context is used to find the data source associated with the connection pool defined in the previous section using the logical name of that pool (jdbc/PoolCloudSparky). The Context.lookup method returns a reference to a Java object, which is narrowed to a javax.sql.PoolDataSource object. Next, the PoolDataSource.getConnection() method is called to establish a connection with the JDBC data source.
* Complete Example of Using a Connection Pool
* Closing the DataDirect Connection Pool