Sample code for end-to-end federated trusted connection scenarios
This sample code illustrates how to use APIs in an application that uses end-to-end federated trusted connections.
An application must use APIs to request an explicit trusted inbound connection and to switch the user on the connection. This sample code illustrates the parts of the application that perform these tasks. For both end-to-end trusted context scenarios, the application is the same.
This excerpt from an application uses the command line interface APIs.
APIs are also available for applications that use Java™ or XA ODBC/CLI.
//Set the trusted connection attribute.
SQLSetConnectAttr(h1, SQL_ATTR_USE_TRUSTED_CONTEXT, SQL_TRUE, SQL_IS_INTEGER);
//Establish a trusted inbound connection for BOSS.
SQLConnect(h1, "testdb", SQL_NTS, "BOSS", SQL_NTS,"*****", SQL_NTS);
//Establish a trusted outbound connection for BOSS.
Perform some work under the user ID BOSS.
SQLExecDirect(hstmt, (unsigned char*)"INSERT INTO PATENTS_NN VALUES...", SQL_NTS);
...
//Commit the work.
SQLEndTran(SQL_HANDLE_DBC, h1, SQL_COMMIT);
//At the transaction boundary, switch the inbound user ID on the trusted
connection to Mary.
SQLSetConnectAttr(h1, SQL_ATTR_TRUSTED_CONTEXT_USERID,"Mary", SQL_IS_POINTER);
//Switch the outbound user ID on the trusted connection to Mary. Perform some
work under the user ID Mary.
SQLExecDirect(*hstmt, (unsigned char*)"INSERT INTO PATENTS_NN VALUES...", SQL_NTS)
...
//Commit the work.
SQLEndTran(SQL_HANDLE_DBC, h1, SQL_COMMIT);
//Disconnect from the database.
SQLDisconnect(h1);