Manually configuring Db2 type 2 connectivity with the Liberty JDBC features

The Liberty JDBC features jdbc-4.0, jdbc-4.1, jdbc-4.2, or jdbc-4.3, can be used to configure JDBC in CICS® Liberty JVM servers to access Db2® databases with type 2 connectivity from Java™ applications. Db2 can be accessed using DataSources or a DriverManager.

Before you begin

Ensure your CICS region has access to the IBM® Data Server Driver for JDBC and SQLJ for Db2 and a CICS Db2 connection (DB2CONN) is defined. For more information, see Configuring a JVM server to support DB2.

About this task

This task explains how to define the elements in server.xml and properties in the JVM profile to connect to a local Db2 database with type 2 connectivity. Your application can connect to Db2 using either DataSources with the javax.sql.DataSource class, or using a DriverManager, with the java.sql.DriverManager class.

Java 17Java 21 To enable Db2 type 2 connectivity when you are running Java 17 or Java 21, add LIBPATH_SUFFIX=/usr/lpp/db2v13/jdbc/lib to the JVM profile.

Procedure

  1. Add the one of the jdbc-4.0, jdbc-4.1, jdbc-4.2, or jdbc-4.3 features to the server.xml file.
    Example configuration to install JDBC 4.2
    <featureManager>
        <feature>jdbc-4.2</feature>
    </featureManager>
  2. Optional: Set the Db2 schema. For more information, see Setting the Db2 schema in a JVM server
  3. Add a library element to the server.xml file to specify the location, on zFS, of the IBM Data Server Driver for JDBC and SQLJ for Db2.
    <library id="db2Library">
        <fileset dir="/usr/lpp/db2v13/jdbc/classes" includes="db2jcc4.jar db2jcc_license_cisuz.jar"/>
        <fileset dir="/usr/lpp/db2v13/jdbc/lib" includes="libdb2jcct2zos4_64.so"/>
    </library>
    This step can be omitted if -Dcom.ibm.cics.jvmserver.wlp.jdbc.driver.location is set in your JVM profile, in which case it is added automatically. For more information, see JVM system property com.ibm.cics.jvmserver.wlp.jdbc.driver.location.
  4. Configure a dataSource element.

    To access Db2 through DataSources or with a DriverManager, a dataSource element must be specified.

    The jndiName attribute of the dataSource defines the JNDI name that is referenced by your application.

    For Db2 type 2 connectivity, the dataSource element must be configured with specific attributes.

    • The transaction attribute must be set to false to allow CICS to manage transactions.
    • A connectionManager element must be specified as a child of the dataSource element, and must have the connectionTimeout attribute set to 0 to disable Liberty connection pooling. The DB2CONN manages connection pooling for CICS Db2 type 2 connectivity.
    • A properties.db2.jcc element must be specified as a child of the dataSource element, and must have the driverType attribute set to 2 to use Db2 type 2 connectivity.
    • From JDBC 4.3, the type attribute must be set to either javax.sql.ConnectionPooledDataSource or javax.sql.DataSource.

    From JDBC 4.3, the default type attribute is javax.sql.XADataSource, which is not supported by the IBM Data Server Driver for JDBC and SQLJ for Db2 with type 2 connectivity.

    Example configuration for a Db2 DataSource with type 2 connectivity.
    <dataSource id="db2Type2" jndiName="jdbc/db2Type2" 
               transactional="false" type="javax.sql.ConnectionPoolDataSource">
        <jdbcDriver libraryRef="db2Type2Driver"/>    
        <properties.db2.jcc driverType="2"/>    
        <connectionManager agedTimeout="0"/>
    </dataSource>

    The dataSource element is also required to be present when accessing Db2 with a DriverManager, even if a DataSource is not used.

    This step can be omitted if -Dcom.ibm.cics.jvmserver.wlp.jdbc.driver.location is set in your JVM profile, in which case it is added automatically. For more information, see JVM system property com.ibm.cics.jvmserver.wlp.jdbc.driver.location.
  5. Optional: Configure the DataSource in properties.db2.jcc element.

    Some of the attributes that can be specified on the properties.db2.jcc element are not valid for DataSources with type 2 connectivity. If an invalid attribute is specified, it is ignored and a warning message is issued.

    The following attributes are not valid:
    • serverName
    • portNumber
    • user
    • password
    • databaseName
  6. To access Db2 with a DriverManager, a library element must be added to your server.xml.
    The library element must have the attribute id set to be the special value global. The library is added to the classpath of all applications installed on the Liberty server, allowing applications to use the Db2 implementation of java.sql.DriverManager.getConnection.
    String connUrl = "jdbc:default:connection"
    Connection driverManagerConn = DriverManager.getConnection(connUrl);

    A fileset element must be specified as a sub element of the library element and have the dir attribute that is the path to the Db2 JDBC classes directory and an includes attribute of db2jcc4.jar.

    <library id="global">
        <fileset dir="/usr/lpp/db2v13/jdbc/classes" includes="db2jcc4.jar db2jcc_license_cisuz.jar"/>
        <fileset dir="/usr/lpp/db2v13/jdbc/lib" includes="libdb2jcct2zos4_64.so"/>
    </library>

Results

The Liberty JVM server can connect to Db2 databases with JDBC type 2 connectivity that uses the CICS DB2CONN resource.

You can look up an instance of javax.sql.DataSource through JNDI in applications. The JNDI name of this DataSource is defined by the jndiName attribute in the dataSource element.

Example server.xml configuration for a Db2 DataSource with type 2 connectivity that uses the Liberty JDBC 4.2 feature

<server>
    <!-- Install JDBC 4.2 -->
    <featureManager>
        <feature>jdbc-4.2</feature>
    </featureManager>

    <!-- Configure the IBM Data Server Driver for JDBC and SQLJ for Db2 driver library -->
    <library id="db2Type2Driver">
        <fileset dir="/usr/lpp/db2v13/jdbc/classes" includes="db2jcc4.jar db2jcc_license_cisuz.jar"/>
        <fileset dir="/usr/lpp/db2v13/jdbc/lib" includes="libdb2jcct2zos4_64.so"/>
    </library>

    <!-- Configure the DataSource -->
    <dataSource id="db2Type2" jndiName="jdbc/db2Type2" transactional="false">
        <jdbcDriver libraryRef="db2Type2Driver"/>    
        <properties.db2.jcc driverType="2"/>    
        <connectionManager agedTimeout="0"/>
    </dataSource>
</server>