Java Database Connectivity
4.3
4.2
4.1
4.0

This feature enables the configuration of DataSources to access Databases from applications. Any JDBC driver that complies with the JDBC 4.3, 4.2, 4.1, 4.0, 3.0, or 2.x specification can be used; customized configuration of many specific providers is included. High performance connection pooling is also provided.

Enabling this feature

To enable the Java Database Connectivity 4.3 feature, add the following element declaration into your server.xml file, inside the featureManager element:

<feature>jdbc-4.3</feature>

Examples

Simple Data source configuration

To configure a data source, the following two configuration elements are typically required:

  • A library element that specifies the location of the JDBC driver file or files

  • One or more dataSource elements that reference the library element

The following example creates a data source that can be injected by using the jdbc/myDataSource name. If the jndi-1.0 feature is enabled, jdbc/myDataSource can be used as a JNDI name:

<library id="JdbcLib">
    <fileset dir="server1/jdbc"/>
</library>

<dataSource jndiName="jdbc/myDataSource">
   <jdbcDriver libraryRef="JdbcLib"/>
   <properties databaseName="myDB" serverName="mydb.mycompany.com" portNumber="50000"/>
</dataSource>

A servlet or enterprise bean can then get the data source injected with code that is similar to the following example:

@Resource(name="jdbc/myDataSource")
private DataSource myDb;

public void insertRochester() throws SQLException {
    try (Connection con = myDataSource.getConnection()) {
       con.createStatement().executeUpdate("INSERT INTO CITIES VALUES('Rochester', 'MN', 'US')");
    }
}

DataSource with authentication

To connect to remote databases, you typically must provide credentials. These credentials can be configured by specifying an authData element. The password can be in plain text, xor, or aes encrypted. You can use the securityUtility encode command to create an encoded password. The following example encodes password as the password value:

<authData id="dbCreds" user="dbUser" password="{aes}AEJrzAGfDEmtxI18U/qEcv54kXmUIgUUV7b5pybw/BzH" />

<dataSource jndiName="jdbc/myDataSource" containerAuthDataRef="dbCreds">
   <jdbcDriver libraryRef="JdbcLib"/>
   <properties databaseName="myDB" serverName="mydb.mycompany.com" portNumber="50000"/>
</dataSource>

Configuration of the default data source

If you enable any Java EE or Jakarta EE features in Open Liberty, you can configure a default data source. To configure a default data source, set the ID of the dataSource element to DefaultDataSource, as shown in the following example:

<dataSource id="DefaultDataSource">
<jdbcDriver libraryRef="jdbcLib"/>
<properties serverName="localhost" portNumber="5432"
            databaseName="myDB"
            user="exampleUser" password="examplePassword"/>
</dataSource>

Using JDBC driver classes from an application

Some applications make use of JDBC driver classes using Connection.unwrap. This means the application needs to be able to view the JDBC driver classes. This is done by configuring the application classloader to see the JDBC driver classes:

<library id="JdbcLib">
    <fileset dir="server1/jdbc"/>
</library>

<webApplication location="myweb.war" >
    <classloader commonLibraryRef="JdbcLib" />
</webApplication>

<dataSource jndiName="jdbc/myDataSource">
   <jdbcDriver libraryRef="JdbcLib"/>
   <properties databaseName="myDB" serverName="mydb.mycompany.com" portNumber="50000"/>
</dataSource>

Connection pool configuration

Connection pooling improves the efficiency of data source connections and operations. Connection pooling for JDBC data sources in Open Liberty is controlled by a connection manager. The connection manager for each data source has sensible default values, but these values can be customized if needed. Any dataSource element can specify an optional connectionManager element to customize the connection pool settings, as shown in the following example:

<dataSource jndiName="jdbc/myDB">
  <jdbcDriver libraryRef="jdbcLib"/>
  <connectionManager maxPoolSize="10" minPoolSize="2"/>
  <properties ... />
</dataSource>

For more information, see Relational database connections with JDBC.

Supported Java versions

  • JavaSE-11.0

  • JavaSE-15.0

Developing a feature that depends on this feature

If you are developing a feature that depends on this feature, include the following item in the Subsystem-Content header in your feature manifest file.

com.ibm.websphere.appserver.jdbc-4.3; type="osgi.subsystem.feature"