Multiple contexts, one Language Environment thread
When you specify the initialization file setting MULTICONTEXT=1, a Db2 ODBC application can create multiple independent connections for each Language Environment® thread.
The following example is an application that uses multiple contexts on one Language Environment thread.
/* 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);
.
.