DB2 Version 9.7 for Linux, UNIX, and Windows

Creating and deploying DataSource objects

JDBC versions starting with version 2.0 provide the DataSource interface for connecting to a data source. Using the DataSource interface is the preferred way to connect to a data source.

Using the DataSource interface involves two parts:
  • Creating and deploying DataSource objects. This is usually done by a system administrator, using a tool such as WebSphere® Application Server.
  • Using the DataSource objects to create a connection. This is done in the application program.
This topic contains information that you need if you create and deploy the DataSource objects yourself.
The IBM® Data Server Driver for JDBC and SQLJ provides the following DataSource implementations:
  • com.ibm.db2.jcc.DB2SimpleDataSource, which does not support connection pooling. You can use this implementation with IBM Data Server Driver for JDBC and SQLJ type 2 connectivity or IBM Data Server Driver for JDBC and SQLJ type 4 connectivity.
  • com.ibm.db2.jcc.DB2ConnectionPoolDataSource, which supports connection pooling. You can use this implementation with IBM Data Server Driver for JDBC and SQLJ type 2 connectivity or IBM Data Server Driver for JDBC and SQLJ type 4 connectivity.
  • com.ibm.db2.jcc.DB2XADataSource, which supports connection pooling and distributed transactions. The connection pooling is provided by WebSphere Application Server or another application server. You can use this implementation only with IBM Data Server Driver for JDBC and SQLJ type 4 connectivity.
The DB2® JDBC Type 2 Driver provides the following DataSource implementations:
  • COM.ibm.db2.jdbc.DB2DataSource, which is enabled for connection pooling. With this implementation, connection pooling is handled internally and is transparent to the application.
  • COM.ibm.db2.jdbc.DB2XADataSource, which does not have built-in support for distributed transactions and connection pooling. With this implementation, you must manage the distributed transactions and connection pooling yourself, either by writing your own code or by using a tool such as WebSphere Application Server.

To create and deploy a DataSource object, you need to perform these tasks:

  1. Create an instance of the appropriate DataSource implementation.
  2. Set the properties of the DataSource object.
  3. Register the object with the Java™ Naming and Directory Interface (JNDI) naming service.

The following example shows how to perform these tasks.

Figure 1. Example of creating and deploying a DataSource object
import java.sql.*;        // JDBC base
import javax.naming.*;    // JNDI Naming Services
import javax.sql.*;       // Additional methods for JDBC
import com.ibm.db2.jcc.*; // IBM Data Server Driver for
                          // JDBC and SQLJ
                          // implementation of JDBC
                          // standard extension APIs

DB2SimpleDataSource dbds = new com.ibm.db2.jcc.DB2SimpleDataSource();   1 

dbds.setDatabaseName("db2loc1");                                        2 
dbds.setDescription("Our Sample Database");
dbds.setUser("john");
dbds.setPassword("mypw");
…
Context ctx=new InitialContext();                                       3 
Ctx.bind("jdbc/sampledb",dbds);                                         4 
Note Description
1 Creates an instance of the DB2SimpleDataSource class.
2 This statement and the next three statements set values for properties of this DB2SimpleDataSource object.
3 Creates a context for use by JNDI.
4 Associates DBSimple2DataSource object dbds with the logical name jdbc/sampledb. An application that uses this object can refer to it by the name jdbc/sampledb.