IBM Books

Call Level Interface Guide and Reference

Sample Asynchronous Application

The following CLI sample, async.c, demonstrates a simple application that runs SQLExecDirect() asynchronously. It is based on the CLI sample program fetch.c.

/* CLI sample async.c */
/* ... */
    /* Make the result from SQLGetInfo() more meaningful by mapping */
    /* the returned value to the string. */
    static char ASYNCMODE[][19] =  {  "SQL_AM_NONE", 
                                      "SQL_AM_CONNECTION", 
                                      "SQL_AM_STATEMENT" };
/* ... */
     * See what type of Asynchronous support is available,
     * and whether or not the CLI/ODBC configuration keyword ASYNCENABLE
     * is set on or off.
     */
    rc = SQLGetInfo( hdbc, /* Connection handle */
                     SQL_ASYNC_MODE, /* Query the support available */
                     &ubuffer, /* Store the result in this variable */
                     4, 
                     &outlen);
    CHECK_STMT(hstmt, rc);
 
    printf("SQL_ASYNC_MODE value from SQLGetInfo() is %s.\n\n",ASYNCMODE[ubuffer]);
 
    if (ubuffer == SQL_AM_NONE ) { /* Async not supported */
       printf("Asynchronous execution is not supported by this datasource\n");
       printf("or has been turned off by the CLI/ODBC configuration keyword\n");
       printf("ASYNCENABLE. The application will continue, but SQLExecDirect()\n");
       printf("will not be run asynchronously.\n\n");
 
       /* There is no need to set the SQLSetStmtAttr() option */
    } else {
 
       /* Set statement level asynchronous execution on */
       rc = SQLSetStmtAttr(
                hstmt,
                SQL_ATTR_ASYNC_ENABLE,
                (SQLPOINTER) SQL_ASYNC_ENABLE_ON,  
                0);
       CHECK_STMT(hstmt, rc); 
    }
 
 
    /* The while loop is new for the asynchronous sample, the */
    /* SQLExecDirect() call remains the same. */ 
    while ((rc = SQLExecDirect(hstmt, sqlstmt, SQL_NTS) ) == SQL_STILL_EXECUTING) {
       printf("   ...SQLExecDirect() still executing asynchronously...\n");
       /* Other processing can be performed here, between each call
        * to see if SQLExecDirect() has finished running asynchronously.
        * This section will never run if CLI runs the function
        * synchronously.
        */
   }
 
    CHECK_STMT(hstmt, rc);
 
    rc = SQLBindCol(hstmt, 1, SQL_C_CHAR, (SQLPOINTER) deptname.s, 15,
                    &deptname.ind);
    CHECK_STMT(hstmt, rc);
 
    rc = SQLBindCol(hstmt, 2, SQL_C_CHAR, (SQLPOINTER) location.s, 15,
                    &location.ind);
    CHECK_STMT(hstmt, rc);
 
    printf("Departments in Eastern division:\n");
    printf("DEPTNAME       Location\n");
    printf("-------------- -------------\n");
 
    while ((rc = SQLFetch(hstmt)) == SQL_SUCCESS) {
        printf("%-14.14s %-14.14s \n", deptname.s, location.s);
    }
    if (rc != SQL_NO_DATA_FOUND)
        check_error(henv, hdbc, hstmt, rc, __LINE__, __FILE__);
 
    rc = SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
    CHECK_STMT(hstmt, rc);
 
    rc = SQLEndTran(SQL_HANDLE_ENV, henv, SQL_COMMIT);
    CHECK_DBC(hdbc, rc);
 
    printf("Disconnecting .....\n");
    rc = SQLDisconnect(hdbc);
    CHECK_DBC(hdbc, rc);
 
    rc = SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
    CHECK_DBC(hdbc, rc);
 
    rc = SQLFreeHandle(SQL_HANDLE_ENV, henv);
    if (rc != SQL_SUCCESS)
        return (terminate(henv, rc));
 
}                               /* end main */


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]