[unixODBC-support] Multi-thread testing

Brian Reynolds Brian.Reynolds at risaris.com
Fri Aug 19 16:09:03 BST 2011


This is the C program I'm using:


#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

#include <sql.h>
#include <sqlext.h>


void *runRequest(void *arg);

SQLHENV hEnv = NULL;

int main() {
     printf("Starting test...");

     SQLRETURN rcSql;

     rcSql = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);

     if (hEnv == NULL || rcSql != SQL_SUCCESS) {
         printf("SQLAllocHandle hEnv error\n");
         return -1;
     }

     rcSql = SQLSetEnvAttr( hEnv, SQL_ATTR_ODBC_VERSION, (void *) 
SQL_OV_ODBC3, 0 );
     if (rcSql != SQL_SUCCESS) {
         printf("SQLSetEnvAttr hEnv error\n");
         return -1;
     }

     const int numSize = 64;

     pthread_t thrArray[numSize];

     for (int i = 0; i != numSize; i++) {
         //printf("Create %d\n", i);
         pthread_create(&thrArray[i], NULL, &runRequest, NULL );
         //sleep(1);
     }

     for (int i = 0; i != numSize; i++) {
         //printf("join %d\n", i);
         pthread_join(thrArray[i], NULL);
         //    printf("joined %d\n", i);
     }


     rcSql = SQLFreeHandle( SQL_HANDLE_ENV, hEnv );
     if (rcSql != SQL_SUCCESS) {
         printf("SQLFreeHandle hEnv error\n");
         return -1;
     }

     printf("Finished Test\n");

}


void *runRequest(void *arg) {

     SQLHDBC hCon = NULL;
     SQLHSTMT hStmt = NULL;
     SQLRETURN rcSql;

     // Errors
     SQLCHAR    retMessage[ 256 ];
     SQLCHAR    retState[ 7 ];
     SQLINTEGER retNativeErrNum;
     SQLSMALLINT retMessageLen;

     rcSql = SQLAllocHandle( SQL_HANDLE_DBC, hEnv, &hCon );
     if (rcSql != SQL_SUCCESS) {

         rcSql = SQLGetDiagRec( SQL_HANDLE_DBC, hCon, 0, retState,
&retNativeErrNum, retMessage, sizeof(retMessage), &retMessageLen );

         printf("SQLAllocHandle hCon error : %s \n", retMessage );
         return NULL;
     }

     const char *myUser = "";
     const char *myPassword = "" ;

     rcSql = SQLConnect( hCon,
                         ( SQLCHAR * ) "qe", SQL_NTS,
                         ( SQLCHAR * ) myUser, SQL_NTS,
                         ( SQLCHAR * ) myPassword, SQL_NTS );

     if (rcSql != SQL_SUCCESS) {

         rcSql = SQLGetDiagRec( SQL_HANDLE_DBC, hCon, 1, retState,
&retNativeErrNum, retMessage, sizeof(retMessage), &retMessageLen );

         rcSql = SQLFreeHandle( SQL_HANDLE_DBC, hCon );

         printf("SQLConnect hCon error %s\n", retMessage );
         return NULL;
     }

     rcSql = SQLAllocHandle( SQL_HANDLE_STMT, hCon, &hStmt );
     if (rcSql != SQL_SUCCESS) {

         rcSql = SQLDisconnect( hCon );
         rcSql = SQLFreeHandle( SQL_HANDLE_DBC, hCon );
         printf("SQLAllocHandle hStmt error\n");
         return NULL;
     }

     // DO STUFF!!

     rcSql = SQLFreeHandle( SQL_HANDLE_STMT, hStmt );
     if (rcSql != SQL_SUCCESS) {
         printf("SQLFreeHandle hCon error\n");
         return NULL;
     }

     rcSql = SQLDisconnect( hCon );
     if (rcSql != SQL_SUCCESS) {
         printf("SQLDisconnect hCon error \n");
         return NULL;
     }

     rcSql = SQLFreeHandle( SQL_HANDLE_DBC, hCon );
     if (rcSql != SQL_SUCCESS) {
         printf("SQLFreeHandle hCon error\n");
         return NULL;
     }

     pthread_exit(NULL);

     return NULL;
}

-- 




More information about the unixODBC-support mailing list