Using a connection pool DataSource object
- Last Updated: June 6, 2018
- 1 minute read
- DataDirect Connectors
- JDBC
- Aha! 6.0
- Amazon Redshift 6.0
- Apache Cassandra 6.0
- Apache Hive 6.0
- Apache Spark SQL 6.0
- Atlassian Jira 6.0
- Autonomous REST Connector 6.0
- Cloudera Impala 5.1
- + 24
Once a PooledConnectionDataSource object has been created and
registered with JNDI, it can be used by your JDBC application as shown in the following
example:
Context ctx = new InitialContext();
ConnectionPoolDataSource ds =
(ConnectionPoolDataSource)ctx.lookup("EmployeeDB");
Connection conn = ds.getConnection("domino", "spark");
The example begins with the intialization of the JNDI environment. Then, the
initial naming context is used to find the logical name of the JDBC
DataSource (EmployeeDB). The
Context.lookup method returns a reference to a Java object, which
is narrowed to a javax.sql.ConnectionPoolDataSource object. Next, the
ConnectionPoolDataSource.getPooledConnection() method is called to
establish a connection with the underlying database. Then, the application obtains a
connection from the ConnectionPoolDataSource.