Connecting to a JDBC Data Source using a connection pool
- Last Updated: April 14, 2020
- 1 minute read
- Hybrid Data Pipeline
- Version 5.0
- Documentation
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/PoolHybridSparky");
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/PoolHybridSparky). 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.