InfoCenter Home > 4.2.4.2.1.1: Creating datasources with the WebSphere connection pooling APIIBM 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:
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:
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. |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|