以下の例は、ODBC 外部のコンテキストを管理する
アプリケーションを示しています。このアプリケーションは、RRS API を使用してコンテキスト・マネージャーとして登録し、
出口ルーチンを設定し、外部コンテキストを作成し、さらにコンテキスト間で切り替えます。
図 1. 外部コンテキストを管理するアプリケーション
/* Register as an unauthorized resource manager */
CRGGRM();
/* Set exit information */
CRGSEIF();
/* Get an environment handle (henv) */
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
/* Get a connection handle, hdbc1, and connect to
STLEC1 under the native context. */
SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
SQLConnect( hdbc1, "STLEC1", ... );
/* Execute SQL under the native context at STLEC1*/
SQLAllocHandle(SQL_HANDLE_STMT, ...);
SQLExecDirect ...
.
.
/* Create a private context */
CTXBEGC( ctxtoken1 );
/* Switch to private */
CTXSWCH( ctxtoken1 );
An application that manages external contexts
/* Get a connection handle, hdbc2, and connect
to STLEC1 under the private context. */
SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc2);
SQLConnect( hdbc2, "STLEC1", ... );
/* Execute SQL under the private context at
STLEC1 */
SQLAllocHandle(SQL_HANDLE_STMT, ...);
SQLExecDirect ...
.
.
/* Commit changes on hdbc2 */
SQLEndTran(SQL_HANDLE_DBC, hdbc2, SQL_COMMIT);
/* Switch back to native */
CTXSWCH( 0 );
/* Execute some more SQL under the native context
at STLEC1 */
SQLAllocHandle(SQL_HANDLE_STMT, ...);
SQLExecDirect ...
.
.
/* Rollback changes on hdbc1 */
SQLEndTran(SQL_HANDLE_DBC, hdbc1, SQL_ROLLBACK);