Acquiring a connection to a database

Before executing SQL statements, a JDBC or SQLJ application must acquire a connection to a database. The application connects to a target data source using one of two Java™ interfaces.

About this task

The term data source can be used in two different contexts, careful attention is needed, as they are distinguished by capitalization:
  • A data source is a source of data such as a database.
  • A DataSource is the Java EE way of encapsulating a set of properties that identify and describe the real world data source that it represents. A DataSource object can be thought of as a factory for connections to the particular data source that it represents.
  • If a DataSource object is registered with a JNDI naming service, an application can use the JNDI API to access that DataSource object, which can then be used to connect to the data source it represents.
  • A DataSource object can choose to implement a connection pool (a set of temporary logical representations of a physical connection). When the application closes such a connection, the temporary connection (also known as a JDBC connection) is not closed, but returned to a pool for re-use.
The two Java interfaces are:
  • DriverManager: This class connects an application to a database which is specified by a database URL.
  • DataSource: This interface is preferred over DriverManager because it allows details about the underlying database to be transparent to your application making applications more portable. The DataSource implementation is created externally to the Java program and obtained with a Java Naming and Directory Interface (JNDI) DataSource name lookup. The DataSource interface is only available in a Liberty JVM server.

In an OSGi JVM server only type 2 connectivity using DriverManager is supported for use with DB2®.

In a Liberty JVM server, you can use the DriverManager or DataSource interfaces to access Db2. To access a local Db2 with type 2 connectivity, use the cicsts:jdbc-1.0 feature. To access a local Db2 or remote Db2 with type 4 connectivity, use the jdbc-4.0 or jdbc-4.1 features provided by Liberty.

If you are using JDBC type 4 connectivity, and want to coordinate database updates with the updates made in the CICS unit of work, use the CICS JTA integration support and perform your updates within the scope of a UserTransaction. See Java Transaction API (JTA) for more information. Additionally you should ensure that your DataSource is an XADataSource (specified by adding the element type="javax.sql.XADataSource " to your definition). If you use the Java EE default DataSource <dataSource id="DefaultDataSource"> it is an XADataSource by default.

If you are using JDBC type 2 connectivity, you do not need to specify a userid and password as the existing CICS® Db2 security procedures are used instead. It is also advisable to use a default URL, for more detail, see Committing a unit of work.

There are JDBC examples for both OSGi and Liberty JVM servers supplied in the IBM® CICS SDK for Java. For more information, see Developing Java applications to run in a Liberty JVM server.

For more information on using the JDBC DriverManager and DataSource interfaces to acquire a connection, and sample code that you can use in your application, see the Db2 for z/OS® : Programming for Java which is appropriate for your version of Db2.