InfoCenter Home >
4: Developing applications >
4.2: Building Web applications >
4.2.4: Accessing data >
4.2.4.2: Obtaining and using database connections >
4.2.4.2.1: Accessing data with the JDBC 2.0 Optional Package APIs >
4.2.4.2.1.1: Creating datasources with the WebSphere connection pooling API

4.2.4.2.1.1: Creating datasources with the WebSphere connection pooling API

IBM WebSphere Application Server provides a public API to enable you to configure a WebSphere datasource in application code. This is necessary only when the application must create a datasource on demand. Otherwise, the datasource is configured by the administrator in the administrative console.

The complete API specification can be found in javadoc for the class com.ibm.websphere.advanced.cm.factory.DataSourceFactory. See the Related information.

To create a datasource on demand in an application, the application must do the following:

  1. Create a Properties object with datasource properties
  2. Obtain a datasource from the factory
  3. Bind the datasource into JNDI

The following code fragment shows how an application would create a datasource and bind it into JNDI:

import com.ibm.websphere.advanced.cm.factory.DataSourceFactory;
...
try {

// Create a properties file for the DataSource
java.util.Properties prop = new java.util.Properties();
prop.put(DataSourceFactory.NAME, "SampleDB");
prop.put(DataSourceFactory.DATASOURCE_CLASS_NAME,
"COM.ibm.db2.jdbc.DB2ConnectionPoolDataSource");
prop.put(DataSourceFactory.DESCRIPTION, "My sample
datasource");
prop.put("databaseName", "sample");

// Obtain a DataSource from the factory
DataSource ds = DataSourceFactory.getDataSource(prop);

// Bind the DataSource into JNDI
DataSourceFactory.bindDataSource(ds);
}
catch (ClassNotFoundException cnfe) {
// check the class path for all necessary classes
}
catch (CMFactoryException cmfe) {
// Example of exception: incorrect properties
}
catch (NamingException ne) {
// Example of exception:  datasource by this name may already exist
}

To create a datasource for binding into JNDI, the application must first create a Properties object to hold the DataSource configuration properties. The only properties required for the datasource from a WebSphere perspective are:

  • NAME -The name of the datasource. This is used to identify the datasource when it is bound into JNDI.
  • DATASOURCE_CLASS_NAME - The complete name of the DataSource class that can be found in the JDBC resource archive file (often referred to as the JDBC provider package). This DataSource class will be used to create connections to the database. The class specified here must implement javax.sql.ConnectionPoolDataSource or javax.sql.XADataSource.

However, depending on the DataSource class specified in the DATASOURCE_CLASS_NAME property, there may be other vendor-specific properties required. In this example, the databaseName property is also required, because DB2ConnectionPoolDataSource is being used. For more information on these vendor-specific properties, see the vendor's documentation for the complete list of properties supported for a datasource.

After a properties object is created, the application can create a new DataSource object by calling getDataSource() on the factory, passing in the Properties object as a parameter. This creates an object of type DataSource, but it is not yet bound into JNDI. To bind a datasource into JNDI, call bindDataSource() on the factory. At this point, other applications can share the datasource by retrieving it from JNDI with the name property specified on creation.

All other APIs specific to connection pooling are not public APIs. Applications that use a WebSphere datasource should follow the JDBC 2.0 Core and JDBC 2.0 Optional Package APIs.

Go to previous article: Accessing data with the JDBC 2.0 Optional Package APIs Go to next article: Tips for using connection pooling

 

 
Go to previous article: Accessing data with the JDBC 2.0 Optional Package APIs Go to next article: Tips for using connection pooling