Configuring relational database connectivity in Liberty

You can configure a data source that is associated with different JDBC providers for database connectivity. The JDBC providers supply the driver implementation classes that are required for JDBC connectivity with your specific vendor database.

About this task

To access a database from your application, you must use a data source. Data sources are provided by JDBC drivers and come in the following varieties:
  • javax.sql.DataSource

    This type of data source is the basic form. It does not provide interoperability that enhances connection pooling, and cannot participate as a two-phase capable resource in transactions that involve multiple resources.

  • javax.sql.ConnectionPoolDataSource

    This type of data source is enabled for connection pooling. It cannot participate as a two-phase capable resource in transactions that involve multiple resources.

  • javax.sql.XADataSource

    This type of data source is both enabled for connection pooling and is able to participate as a two-phase capable resource in transactions that involve multiple resources.

To be usable in Liberty, your JDBC driver must provide at least one of these types of data sources. For the commonly used JDBC drivers, Liberty is already aware of the implementation class names for the various data source types. You need to tell Liberty only where to find the JDBC driver.

Procedure

  1. In the server.xml file, define a shared library that points to the location of your JDBC driver JAR or compressed files. For example:
    <library id="DB2JCC4Lib">
        <fileset dir="C:/DB2/java" includes="db2jcc4.jar db2jcc_license_cisuz.jar"/>
    </library>
  2. Define a data source by using the JDBC driver. If you don't specify the type of data source, Liberty chooses the data source in the following order depending on which is available.
    • javax.sql.ConnectionPoolDataSource
    • javax.sql.DataSource
    • javax.sql.XADataSource
    An example that accepts the default for data source type:
    <dataSource id="db2" jndiName="jdbc/db2">
        <jdbcDriver libraryRef="DB2JCC4Lib"/>
        <properties.db2.jcc databaseName="SAMPLEDB" serverName="localhost" portNumber="50000"/>
    </dataSource>
    An example that uses javax.sql.XADataSource type:
    <dataSource id="db2xa" jndiName="jdbc/db2xa" type="javax.sql.XADataSource">
        <jdbcDriver libraryRef="DB2JCC4Lib"/>
        <properties.db2.jcc databaseName="SAMPLEDB" serverName="localhost" portNumber="50000"/>
    </dataSource>
    A default data source is available when at least one Java EE 7 feature is enabled. This data source uses a different priority to determine the type if none is specified.
    • javax.sql.XADataSource
    • javax.sql.ConnectionPoolDataSource
    • javax.sql.DataSource
    This data source is available as java:comp/DefaultDataSource. A jndiName does not need to be specified for it. To configure the default data source, specify a data source with the id set to DefaultDataSource. An example that configures the default data source to point at a DB2 database:
    <dataSource id="DefaultDataSource">
    	<jdbcDriver libraryRef="DB2JCC4Lib"/>
    	<properties.db2.jcc databaseName="SAMPLEDB" serverName="localhost" portNumber="50000"/>
    </dataSource>
  3. Optional: Configure attributes for the data source, such as JDBC vendor properties and connection pooling properties.
    For example:
    <dataSource id="DefaultDataSource" jndiName="jdbc/db2" connectionSharing="MatchCurrentState" 
                isolationLevel="TRANSACTION_READ_COMMITTED" statementCacheSize="20">
        <connectionManager maxPoolSize="20" minPoolSize="5" 
                           connectionTimeout="10s" agedTimeout="30m"/>
        <jdbcDriver libraryRef="DB2JCC4Lib"/>
        <properties.db2.jcc databaseName="SAMPLEDB" serverName="localhost" portNumber="50000" 
                            currentLockTimeout="30s" user="user1" password="pwd1"/>
    </dataSource>
    For a full list of configuration attributes for the dataSource element, connectionManager element and some commonly used JDBC vendors, see Data Source (dataSource).
  4. Optional: Configure data sources for commonly used databases according to the following examples.
    For DB2®
    <dataSource id="DefaultDataSource" jndiName="jdbc/db2">
        <jdbcDriver libraryRef="DB2JCC4Lib"/>
        <properties.db2.jcc databaseName="SAMPLEDB" serverName="localhost" portNumber="50000"/>
    </dataSource>
    
    <library id="DB2JCC4Lib">
        <fileset dir="C:/DB2/java" includes="db2jcc4.jar db2jcc_license_cisuz.jar"/>
    </library>
    For DB2 on iSeries (Native)
    <dataSource id="DefaultDataSource" jndiName="jdbc/db2iNative">
        <jdbcDriver libraryRef="DB2iNativeLib"/>
        <properties.db2.i.native databaseName="*LOCAL"/>
    </dataSource>
    
    <library id="DB2iNativeLib">
        <fileset dir="/QIBM/Proddata/java400/jdk6/lib/ext" includes="db2_classes16.jar"/>
    </library>
    For DB2 on iSeries (Toolbox)
    <dataSource id="DefaultDataSource" jndiName="jdbc/db2iToolbox">
        <jdbcDriver libraryRef="DB2iToolboxLib"/>
        <properties.db2.i.toolbox databaseName="SAMPLEDB" serverName="localhost"/>
    </dataSource>
    
    <library id="DB2iToolboxLib">
        <fileset dir="/QIBM/ProdData/Http/Public/jt400/lib" includes="jt400.jar"/>
    </library>
    For Derby Embedded
    <dataSource id="DefaultDataSource" jndiName="jdbc/derbyEmbedded">
        <jdbcDriver libraryRef="DerbyLib"/>
        <properties.derby.embedded databaseName="C:/databases/SAMPLEDB" createDatabase="create"/>
    </dataSource>
    
    <library id="DerbyLib">
        <fileset dir="C:/db-derby-10.8.1.2-bin/lib"/>
    </library>
    For Derby Network Client
    <dataSource id="DefaultDataSource" jndiName="jdbc/derbyClient">
        <jdbcDriver libraryRef="DerbyLib"/>
        <properties.derby.client databaseName="C:/databases/SAMPLEDB" createDatabase="create" 
                                 serverName="localhost" portNumber="1527"/>
    </dataSource>
    
    <library id="DerbyLib">
        <fileset dir="C:/db-derby-10.8.1.2-bin/lib"/>
    </library>
    For Informix® JCC
    <dataSource id="DefaultDataSource" jndiName="jdbc/informixjcc">
        <jdbcDriver libraryRef="DB2JCC4Lib"/>
        <properties.informix.jcc databaseName="SAMPLEDB" serverName="localhost" portNumber="1526"/>
    </dataSource>
    
    <library id="DB2JCC4Lib">
        <fileset dir="C:/Drivers/jcc/4.8" includes="db2jcc4.jar db2jcc_license_cisuz.jar"/>
    </library>
    For Informix JDBC
    <dataSource id="DefaultDataSource" jndiName="jdbc/informix">
        <jdbcDriver libraryRef="InformixLib"/>
        <properties.informix databaseName="SAMPLEDB" ifxIFXHOST="localhost" 
                             serverName="ol_machinename" portNumber="1526"/>
    </dataSource>
    
    <library id="InformixLib">
        <fileset dir="C:/Drivers/informix" includes="ifxjdbc.jar ifxjdbcx.jar"/>
    </library>
    For Microsoft SQL Server (Microsoft JDBC driver)
    <dataSource id="DefaultDataSource" jndiName="jdbc/mssqlserver">
        <jdbcDriver libraryRef="MSJDBCLib"/>
        <properties.microsoft.sqlserver databaseName="SAMPLEDB" 
                                        serverName="localhost" portNumber="1433"/>
    </dataSource>
    
    <library id="MSJDBCLib">
        <fileset dir="C:/sqljdbc_6.0/enu/sqljdbc41.jar"/>
    </library>
    For Microsoft SQL Server (DataDirect Connect for JDBC driver)
    <dataSource id="DefaultDataSource" jndiName="jdbc/ddsqlserver">
        <jdbcDriver libraryRef="DataDirectLib"/>
        <properties.datadirect.sqlserver databaseName="SAMPLEDB" 
                                         serverName="localhost" portNumber="1433"/>
    </dataSource>
    
    <library id="DataDirectLib">
        <fileset dir="C:/DataDirect/Connect-4.2/lib/sqlserver.jar"/>
    </library>
    For MySQL
    <dataSource id="DefaultDataSource" jndiName="jdbc/mySQL">
        <jdbcDriver libraryRef="MySQLLib"/>
        <properties databaseName="SAMPLEDB" serverName="localhost" portNumber="3306"/>
    </dataSource>
    
    <library id="MySQLLib">
        <fileset dir="C:/mysql-connector-java-x.x.xx/mysql-connector-java-x.x.xx.jar"/>
    </library>
    For Oracle
    <dataSource id="DefaultDataSource" jndiName="jdbc/oracle">
        <jdbcDriver libraryRef="OracleLib"/>
        <properties.oracle URL="jdbc:oracle:thin:@//localhost:1521/SAMPLEDB"/>
    </dataSource>
    
    <library id="OracleLib">
        <fileset dir="C:/Oracle/lib/ojdbc6.jar"/>
    </library>
    For Sybase
    <dataSource id="DefaultDataSource" jndiName="jdbc/sybase">
        <jdbcDriver libraryRef="SybaseLib"/>
        <properties.sybase databaseName="SAMPLEDB" serverName="localhost" portNumber="5000"/>
    </dataSource>
    
    <library id="SybaseLib">
        <fileset dir="C:/Drivers/sybase/jconn4.jar"/>
    </library>
    For solidDB®
    <dataSource id="DefaultDataSource" jndiName="jdbc/solidDB">
        <jdbcDriver libraryRef="solidLib"/>
        <properties databaseName="SAMPLEDB" URL="jdbc:solid://localhost:2315/"/>
    </dataSource>
    
    <library id="solidLib">
        <fileset dir="C:/Drivers/solidDB/SolidDriver2.0.jar"/>
    </library>
    For a JDBC driver that is not known to Liberty
    <dataSource id="DefaultDataSource" jndiName="jdbc/sample" type="javax.sql.XADataSource">
        <jdbcDriver libraryRef="SampleJDBCLib" 
                    javax.sql.XADataSource="com.ibm.sample.SampleXADataSource"/>
        <properties databaseName="SAMPLEDB" hostName="localhost" port="12345"/>
    </dataSource>
    
    <library id="SampleJDBCLib">
        <fileset dir="C:/Drivers/SampleJDBC/sampleDriver.jar"/>
    </library>
    In the example, the JDBC driver is at C:/Drivers/SampleJDBC/sampleDriver.jar and provides an implementation of javax.sql.XADataSource named com.ibm.sample.SampleXADataSource. The JDBC driver also provides vendor-specific data source properties such as databaseName, hostName, and port.

Icon that indicates the type of topic Task topic



Timestamp icon Last updated: Saturday, 3 December 2016
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twlp_dep_configuring_ds
File name: twlp_dep_configuring_ds.html