Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC) is a Java API for connecting and interactive with relational databases.
The JDBC features are listed in the following table:
| Feature | Support | Supported Java Versions | Supported Enterprise Java Versions |
|---|---|---|---|
jdbc-4.0 |
JDBC version 4.0 |
|
|
jdbc-4.1 |
JDBC version 4.1 |
|
|
jdbc-4.2 |
JDBC version 4.2 |
|
|
| jdbc-4.3 | JDBC version 4.3 |
|
|
cicsts:jdbc-1.0 |
CICS® support for JDBC for Db2® type 2 connections |
|
|
For more information about configuring JDBC support, see Configuring database connectivity with JDBC.
Developing applications with database interaction
There are two methods of interacting with the Db2 database.
- Data Source
-
The
javax.sql.DataSourcecan be looked up in JNDI by using the jndiName defined in the dataSource or cicsts_dataSource configuration element. Thisjavax.sql.DataSourceobject can be used to get ajava.sql.Connectionobject to interact with the database.Depending on the implementation of
javax.sql.DataSource, it might be important to commit or roll back the connection before closing. For more information, see Differences between the Liberty JDBC features and the CICS JDBC feature. - Driver Manager
-
The
java.sql.DriverManagerclass can be used to getjava.sql.Connectionto interact with the database. The connection URL isjdbc:default:connectionfor type 2 connectivity in CICS. For more information, see Connecting to a data source using the DriverManager interface with the IBM Data Server Driver for JDBC and SQLJ
Differences between the Liberty JDBC features and the CICS JDBC feature
Liberty provides JDBC features jdbc-4.0,
jdbc-4.1, jdbc-4.2, and
jdbc-4.3 to connect to various relational databases. In CICS Liberty, Liberty JDBC support can be used to connect to a Db2 database through the CICS DB2CONN with type 2 connectivity, or remotely with a type 4 connectivity. Liberty JDBC supports the java.sql and javax.sql APIs
and provides implementations of javax.xml.DataSource through the
dataSource configuration.
cicsts:jdbc-1.0 to support type 2 connectivity through the CICS DB2CONN. CICS supports the java.sql API, and provides an implementation of
javax.sql.DataSource through the
cicsts_dataSource configuration.batch-1.0
persistence.CICS and Liberty JDBC support transactions that use the JDBC API. For JDBC type 2 connectivity, Db2 updates are managed as part of the CICS unit of work through the CICS DB2CONN resource. Liberty and CICS JDBC differ in how connections are managed.
Liberty JDBC will automatically close the connection after processing completes, causing the
transaction to be rolled back or committed, depending on the
commitOrRollbackOnCleanup attribute of the dataSource
element. The default setting is rollback, so an implicit
java.sql.Connection.commit() must be called, causing the CICS unit of work to be committed.
CICS JDBC is managed by the CICS unit of work. The database updates are committed or rolled back when the CICS task commits or rolls back.
| Type | Connection Type | Autocommit | Default Clean Behavior |
|---|---|---|---|
| CICS JDBC | Type 2 | false |
Commit the CICS UOW. |
| Liberty JDBC | Type 2 | false |
Roll back the CICS UOW. |
| Type 4 | true or false |
Commit the local transaction. The CICS UOW is not affected. |
A global transaction can be used to manage the transactional scope data sources. In global transactions, the transactional behavior for Liberty and CICS javax.sql.DataSource is consistent. For more information about
global transactions in Liberty, see Java Transaction API (JTA).
Examples
Connecting to a database with thejavax.sql.DataSource interface, with a
dataSource or cicsts_dataSource configuration specifying
the jndiName
jdbc/defaultCICSDataSource.import java.sql.Connection;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.sql.DataSource;
public class DataSourceExample
{
// Inject the DataSource resource using JNDI
@Resource(name = "jdbc/defaultCICSDataSource")
private DataSource dataSource;
public void accessDb() throws SQLException
{
try(Connection conn = dataSource.getConnection())
{
// Interact with SQL using the Connection object
}
}
}java.sql.DriverManager class that uses the
cicsts_jdbcDriver
configuration.import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DriverManagerExample
{
public void accessDatabase() throws SQLException
{
try(Connection conn = DriverManager.getConnection("jdbc:default:connection"))
{
// Interact with SQL using the Connection object
}
}
}