Java Database Connectivity
4.3
4.2
4.1
4.0

This feature enables the configuration of DataSources to access Databases from applications. Any database that complies with the JDBC 4.0 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.0 feature, add the following element declaration into your server.xml file, inside the featureManager element:

<feature>jdbc-4.0</feature>

Examples

Configure a data source

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')");
    }
}

Provide security credentials for data source 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>

For information about Kerberos and SPNEGO authentication for JDBC data sources, see Kerberos authentication for JDBC data sources

Specify a 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>

Enable an application to access to JDBC driver classes

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>

Customize connection pool settings

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>

You can define multiple data sources and associate each with a different connection manager. However, you cannot associate multiple data sources with a single connection manager. Using thread local storage for connections can increase performance for applications on multi-threaded systems. When you set the numConnectionsPerThreadLocal attribute for the connectionManager element to 1 or more, these connections per thread are stored in thread local storage. This setting can provide a major improvement on large multi-core (8+) machines by reserving the specified number of database connections for each thread.

Features that this feature enables

Supported Java versions

  • JavaSE-1.8

  • JavaSE-11.0

  • JavaSE-17.0

  • JavaSE-20.0

Features that enable this feature

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.0; type="osgi.subsystem.feature"