Configure database connectivity in the Liberty profile
We can configure a data source associated with different JDBC providers for database connectivity. The JDBC providers supply the driver implementation classes required for JDBC connectivity with the specific vendor database.
To access a database from the application, use a data source. Data sources are provided by JDBC drivers and come in the following varieties:
- javax.sql.DataSource
This is the basic form of a data source. It does not provide interoperability that enhances connection pooling, and cannot participate as a two-phase capable resource in transactions involving 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 involving 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 involving multiple resources.
To be usable in the Liberty profile, the JDBC driver must provide at least one of these types of data sources. For the commonly used JDBC drivers, the Liberty profile is already aware of the implementation class names for the various data source types. You only need to tell the Liberty profile where to find the JDBC driver.
- In server.xml, define a shared library pointing to the location of the JDBC driver JAR or compressed files. For example:
<library id="DB2JCC4Lib"> <fileset dir="C:/DB2/java" includes="db2jcc4.jar db2jcc_license_cisuz.jar"/> </library>
- Define a data source using the JDBC driver. If we don't specify the type of data source, the Liberty profile chooses in the following order depending on which is available.
- javax.sql.ConnectionPoolDataSource
- javax.sql.DataSource
- javax.sql.XADataSource
Here is 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>Here is 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>
- Optional: Configure attributes for the data source, such as JDBC vendor properties and connection pooling properties.
For example:
<dataSource id="db2" 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 Configuration elements in server.xml.
- Optional: Configure data sources for commonly used databases according to the following examples.
- For DB2
<dataSource id="db2" 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="db2iNative" 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="db2iToolbox" 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="derbyEmbedded" 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="derbyClient" 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="informixjcc" 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="informix" 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="mssqlserver" jndiName="jdbc/mssqlserver"> <jdbcDriver libraryRef="MSJDBCLib"/> <properties.microsoft.sqlserver databaseName="SAMPLEDB" serverName="localhost" portNumber="1433"/> </dataSource> <library id="MSJDBCLib"> <fileset dir="C:/sqljdbc_4.0/enu" includes="sqljdbc4.jar"/> </library>
- For Microsoft SQL Server (DataDirect Connect for JDBC driver)
<dataSource id="ddsqlserver" 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" includes="sqlserver.jar"/> </library>
- For MySQL
<dataSource id="mySQL" 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" includes="mysql-connector-java-x.x.xx.jar"/> </library>
- For Oracle
<dataSource id="oracle" jndiName="jdbc/oracle"> <jdbcDriver libraryRef="OracleLib"/> <properties.oracle URL="jdbc:oracle:thin:@//localhost:1521/SAMPLEDB"/> </dataSource> <library id="OracleLib"> <fileset dir="C:/Oracle/lib" includes="ojdbc6.jar"/> </library>
- For Sybase
<dataSource id="sybase" jndiName="jdbc/sybase"> <jdbcDriver libraryRef="SybaseLib"/> <properties.sybase databaseName="SAMPLEDB" serverName="localhost" portNumber="5000"/> </dataSource> <library id="SybaseLib"> <fileset dir="C:/Drivers/sybase" includes="jconn4.jar"/> </library>
- For solidDB
<dataSource id="solidDB" jndiName="jdbc/solidDB"> <jdbcDriver libraryRef="solidLib"/> <properties databaseName="SAMPLEDB" URL="jdbc:solid://localhost:2315/"/> </dataSource> <library id="solidLib"> <fileset dir="C:/Drivers/solidDB" includes="SolidDriver2.0.jar"/> </library>
- For a JDBC driver that is not known to the Liberty profile
<dataSource id="sample" 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/" includes="sampleDriver.jar"/> </library>In the example, the JDBC driver is located 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.
Subtopics
- How data source configuration updates are applied
If we change the attributes of the dataSource element while a server is running, the updates to different attributes are applied at different times and in different ways.
- Application-defined data sources
We can define a data source within the application, through annotations or in the deployment descriptor, as defined by the Java EE specification.
- Configure connection pooling for database connections
We can configure connection pooling for the data source by defining a connection manager for it.
Parent topic: Administer data access applicationsTasks:
Configure session persistence