Db2 ODBC support for multiple Language Environment threads

A Language Environment® thread represents an independent instance of a routine within an application. When you execute a Db2 ODBC application, it begins with an initial Language Environment thread, or parent thread.

To make your application multithreaded, call the POSIX Pthread function pthread_create() within your application. This function creates additional Language Environment threads, or child threads, which work concurrently with the parent thread.

You must run multithreaded Db2 ODBC applications in one of the following environments:
  • The z/OS® UNIX environment.
  • For applications that are HFS-resident, TSO or batch environments that use the IBM®-supplied BPXBATCH program.
  • For applications that are not HFS-resident, TSO or batch environments that use the Language Environment run time option POSIX(ON).
    Example: To run the multithreaded, non-HFS, Db2 ODBC application APP1 in the data set USER.RUNLIB.LOAD, you could use one of the following approaches:
    • Use TSO to enter the command:
      CALL 'USER.RUNLIB.LOAD(APP1)' 'POSIX(ON)/'
    • Use batch JCL to submit the job:
      //STEP1 EXEC PGM=APP1,PARM='POSIX(ON)/'
      //STEPLIB DD DSN=USER.RUNLIB.LOAD,DISP=SHR
      //        DD ...other libraries needed at run time...

The collection of all the Language Environment threads in an application make an independent set of routines called a Language Environment enclave. All Language Environment threads within an enclave share the same reentrant copy of the Db2 ODBC driver code. Db2 ODBC must also protect shared storage when multiple Language Environment threads run concurrently in the same enclave. Reentrant code that correctly handles shared storage is referred to as threadsafe. Multithreaded ODBC applications require a threadsafe driver.

The Db2 ODBC driver is threadsafe. Db2 ODBC supports the concurrent execution of Language Environment threads. Your Db2 ODBC applications will support multiple Language Environment threads, only if the following conditions are true:
  • Db2 ODBC can access the z/OS UNIX environment. Db2 ODBC uses Pthread mutex functions, which the z/OS UNIX environment provides, to serialize critical sections of Db2 ODBC code. With these Pthread mutex functions, all Db2 ODBC functions are threadsafe.
  • THREADSAFE=0 is not specified in the initialization file. You can use the THREADSAFE keyword to specify whether the Db2 ODBC driver uses Pthread mutex functions to make your applications threadsafe.

Multithreaded applications use threads to perform work in parallel. The following figure depicts an application that performs parallel operations on two different connections and manages a shared application buffer.

Figure 1. Multithreaded application
Begin figure summary. This figure contains the pseudocode for a parent thread that creates two child Language Environment threads. Detailed description available.
The application that the preceding figure portrays an application that performs the following steps to make a parallel database-to-database copy:
  1. Creates two child Language Environment threads from an initial parent thread. The parent thread remains active for the duration of the child threads. Db2 ODBC requires that the thread that establishes the environment handle must persist for the duration of the application. The persistence of this thread keeps Db2 language interface routines resident in the Language Environment enclave.
  2. Connects to database A with child Language Environment thread 1 and uses SQLFetch() to read data from this connection into a shared application buffer.
  3. Connects to database B with child Language Environment thread 2. Child Language Environment thread 2 concurrently reads data from the shared application buffer and inserts this data into database B.
  4. Calls Pthread functions to synchronize the use of the shared application buffer within each of the child threads.