Java Database Connectivity4.34.24.14.0
This feature enables the configuration of DataSources to access Databases from applications. Any JDBC driver that complies with the JDBC 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.2 feature, add the following element declaration into your server.xml
file, inside the featureManager
element:
<feature>jdbc-4.2</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 filesOne or more
dataSource
elements that reference thelibrary
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.
For more information, see Relational database connections with JDBC.