Acquiring a connection to a database

Before executing SQL statements, a JDBC or SQLJ application has to acquire a connection or connection context to a database. The database is identified by a database Uniform Resource Locator (URL) that is provided to the JDBC driver. The JDBC driver recognizes two types of URL:

Default URL
A default URL does not include the location name of a DB2® subsystem. A default URL for DB2 for OS/390® and z/OS® can be specified in one of two formats:
jdbc:db2os390sqlj:
or
jdbc:default:connection
When a default URL is specified, the application is given a connection to the local DB2 to which CICS® is connected. If your installation uses DB2 data sharing, you can access all the data in your sysplex from the local DB2.
Explicit URL
An explicit URL includes the location name of a DB2 subsystem. The basic structure of an explicit URL for DB2 for OS/390 and z/OS is:
jdbc:db2os390:<location-name>
or
jdbc:db2os390sqlj:<location-name>
Typically, the location name is the name of the local DB2 to which CICS is connected. However, you can specify the name of a remote DB2 to access. In this case, CICS uses the local DB2 as a pass-through, and uses DB2 Distributed Data Facilities to access the remote DB2.

It is recommended that you use a default URL in a CICS environment. The use of an explicit URL causes particular behaviours at the close of a connection, that could be inconvenient when multiple programs are used in the same application suite. Also, when a default URL is used, the connection behaves in the same way using the JDBC 1.2 driver, the JDBC 2.0 driver, or the DB2 Universal JDBC Driver. See Committing a unit of work for further information.

To acquire a connection to a database, applications need to provide the default or explicit URL of the database to the JDBC driver. The application can provide this URL using one of two methods, depending on the level of JDBC driver that is provided by your DB2 system. It can use the JDBC DriverManager interface, which is supported by the JDBC 1.2 driver, the JDBC 2.0 driver, and the DB2 Universal JDBC Driver; or it can use the DataSource interface, which is supported by the JDBC 2.0 driver and the DB2 Universal JDBC Driver. For descriptions of these two methods, see:

If you can use the JDBC 2.0 driver or the DB2 Universal JDBC Driver, the DataSource interface is the recommended method of acquiring a connection, because applications are then isolated from the platform-specific and JDBC driver-specific mechanisms for obtaining a connection to a database.

How many connections can you have?

A Java™ application for CICS can have at most one JDBC connection or SQLJ connection context open at a time. Although JDBC allows an application to have multiple connections at the same time, CICS does not permit this. However, an application can close an existing connection and open a connection to a new DB2 location.

An application that has an open connection should close the connection before linking to another application that wants to use JDBC or SQLJ. For Java programs that are part of an application suite, you need to consider the implications of closing the connection, because if you are using an explicit URL, closing the connection can cause a syncpoint to be taken. If you are using a default URL, a syncpoint does not have to be taken when the connection is closed. See Committing a unit of work for more information about this.

Related tasks
Programming with JDBC and SQLJ in the CICS DB2 environment
Acquiring a connection using the JDBC DriverManager interface
Acquiring a connection using the DataSource interface
Committing a unit of work
CICS abends during JDBC or SQLJ requests
Using JDBC and SQLJ in enterprise beans: special considerations
[[ Contents Previous Page | Next Page Index ]]