複数コンテキスト、1 つの言語処理環境のスレッド

初期化ファイルの設定値 MULTICONTEXT=1 を指定すると、Db2® ODBC アプリケーション は、それぞれの言語処理環境のスレッドごとに、複数の独立した接続を作成可能 です。

以下の例は、1 つの言語処理環境のスレッドで複数のコンテキストを 使用するアプリケーションです。

図 1. 単一の言語処理環境のスレッドで独立の接続を行うアプリケーション
 
  /*  Get an environment handle (henv).             */
  SQLAllocHandle(SQL_HANDLE_ENV, SQL_HANDLE_NULL, &henv );
  /*
   *  Get two connection handles, hdbc1 and hdbc2, which
   *  represent two independent DB2 threads.
   */
  SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1 );
  SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc2 );
  /*  Set autocommit off for both connections.     */
  /*  This is done only to emphasize the           */
  /*  independence of the connections for purposes */
  /*  of this example, and is not intended as      */
  /*  a general recommendation.                    */
  SQLSetConnectAttr(hdbc1, SQL_ATTR_AUTOCOMMIT, (void *)SQL_AUTOCOMMIT_OFF, 0 );
  SQLSetConnectAttr(hdbc2, SQL_ATTR_AUTOCOMMIT, (void*)SQL_AUTOCOMMIT_OFF, 0 );
  /*  Perform SQL under DB2 thread 1 at STLEC1.    */
  SQLConnect( hdbc1, (SQLCHAR *) "STLEC1", ... );
  SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);
  SQLExecDirect ...
        .
        .
  /*  Perform SQL under DB2 thread 2 at STLEC1.    */
  SQLConnect( hdbc2, (SQLCHAR *) "STLEC1", ... );
  SQLAllocHandle(SQL_HANDLE_STMT, hdbc2, &hstmt2);
  SQLExecDirect ...
       .
       .
  /*  Commit changes on connection 1.              */
  SQLEndTran(SQL_HANDLE_DBC, hdbc1, SQL_COMMIT);
  /*  Rollback changes on connection 2.            */
  SQLEndTran(SQL_HANDLE_DBC, hdbc2, SQL_ROLLBACK);
       .
       .