Table of Contents

Pool

Code Design - Connection Pool in Jdbc

Implementation: https://github.com/swaldman/c3p0

Data Source

The DataSource interface, introduced in JDBC 2.0 Optional Package, is connection factory.

Data source provides connection pooling of connection (ie DriverManager).

Connection conn = dataSource.getConnection(user, passwd);

J2EE/ JNDI

The Java Naming and Directory Interface (JNDI) API provides a uniform way for applications to access remote services over the network

Using the JNDI API, applications can access a DataSource object by specifying its logical name. A naming service using the JNDI API maps this logical name to a corresponding data source. This scheme greatly enhances portability because any of the DataSource properties, such as portNumber or serverName, can be changed without impacting the JDBC client code. This is particularly useful in the three-tier environment (J2ee)

Use of a JNDI-based naming service to deploy a new VendorDataSource object.

Registering a DataSource object with a JNDI-based naming service

// Create a VendorDataSource object and set some properties
VendorDataSource vds = new VendorDataSource();
vds.setServerName("my_database_server");
vds.setDatabaseName("my_database");
vds.setDescription("data source for inventory and personnel");
// Use the JNDI API to register the new VendorDataSource object.
// Reference the root JNDI naming context and then bind the
// logical name "jdbc/AcmeDB" to the new VendorDataSource object.
Context ctx = new InitialContext();
ctx.bind("jdbc/AcmeDB", vds);