OCI編程高級篇(二十二) OCI多線程編程和示例代碼
訪問www.tomcoding.com網站,學習Oracle內部數據結構,詳細文檔說明,下載Oracle的exp/imp,DUL,logminer,ASM工具的源代碼,學習高技術含量的內容。
在前面我們介紹連接池時,用了單線程編寫了一個實例,這不能體現出連接池的優勢,連接池更應該用在多線程的環境中。我們介紹了多線程的函數后,今天就用多線程改寫一下原來的例子。
還是先創建連接池,然后創建并啟動4個線程,每個線程賦值一個數字,然后通過數據庫的dual來取得數字,打印出來,用dual來取數目的是為了使用連接池的會話。示例代碼如下。
#include "stdio.h" #include "stdlib.h" #include "stdint.h" #include "memory.h" #include "string.h" #include "time.h" #include "errno.h" #include "oci.h" /* 定義全局變量 */ OCIEnv *envhp = NULL; /* OCI環境句柄 */ OCIError *errhp = NULL; /* 錯誤句柄 */ OCICPool *poolhp = NULL; /* 連接池句柄 */ sb4 pool_name_len; /* 連接池名稱長度 */
char *pool_name = NULL; /* 連接池名稱 */
struct { char uname[32]; /* 認證用戶名 */ char upwd[32]; /* 認證用戶密碼 */ } inputs;
/* 打印使用說明 */
static void usage(const char *prg){ fprintf(stderr, "Usage: %s user/password\n\n" " user oracle user\n" " password user password\n" , prg ); } /* 解析命令行參數 */
static int parse_inputs(int argc, char *argv[]){ char *p; if (argc < 2) { usage(argv[0]); return (-1); } if ((p=strchr(argv[1], '/')) == NULL) { usage(argv[0]); return (-1); } *p++ = '\0'; memset(&inputs, 0, sizeof(inputs)); strncpy(inputs.uname, argv[1], 31); strncpy(inputs.upwd, p, 31); return (0); } static char ora_env[][16] = { "ORACLE_HOME", "ORACLE_SID", "" }; /* 檢查程序運行的Oracle環境變量 */
static int check_ora_env(void){ int i; for (i=0; ora_env[i][0]; i++) { if (getenv(ora_env[i]) == NULL) { fprintf(stderr, "env %s not set.\n", ora_env[i]); return (-1); } } return (0); } /* 創建連接池函數 */
static int create_connection_pool(void){ sword rc; sb4 ec; ub4 conn_min; ub4 conn_max; ub4 conn_inc; OraText errbuf[512]; /* 創建OCI環境變量,要使用OCI_THREADED模式 */ rc = OCIEnvCreate( &envhp, /* envhpp */ OCI_THREADED, /* mode */ (void *)NULL, /* ctxp */ NULL, NULL, NULL, (size_t)0, (void **)NULL ); if (rc != OCI_SUCCESS) { fprintf(stderr, "OCIEnvCreate() - allocate OCI env handle error !\n"); return (-1); } /* 分配錯誤句柄 */ rc = OCIHandleAlloc( (void *)envhp, (void **)&errhp, OCI_HTYPE_ERROR, 0, (void **)NULL ); if (rc != OCI_SUCCESS) { fprintf(stderr, "OCIHandleAlloc() - allocate OCI error handle error !\n"); return (-1); } /* 分配連接池句柄 */ rc = OCIHandleAlloc( (void *)envhp, (void **)&poolhp, OCI_HTYPE_CPOOL, (size_t)0, (void **)NULL ); if (rc != OCI_SUCCESS) { fprintf(stderr, "OCIHandleAlloc() - allocate connection pool handle error !\n"); return (-1); } /* 創建連接池 */ conn_min = 3; conn_max = 5; conn_inc = 1; rc = OCIConnectionPoolCreate( envhp, errhp, poolhp, (OraText **)&pool_name, &pool_name_len, (const OraText *)"", 0, (ub4)conn_min, (ub4)conn_max, (ub4)conn_inc, (OraText *)inputs.uname, strlen(inputs.uname), (OraText *)inputs.upwd, strlen(inputs.upwd), OCI_DEFAULT ); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIConnectionPoolCreate() - [%d] %s\n", ec, errbuf); return (-1); } return (0); } /* 銷毀連接池函數 */
static int destroy_connection_pool(void){ sword rc; sb4 ec; OraText errbuf[512]; /* 銷毀連接池 */ if (poolhp != NULL) { rc = OCIConnectionPoolDestroy(poolhp, errhp, OCI_DEFAULT); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIConnectionPoolDestroy() - [%d] %s\n", ec, errbuf); return (-1); } /* 釋放連接池句柄 */ rc = OCIHandleFree((void *)poolhp, OCI_HTYPE_CPOOL); if (rc != OCI_SUCCESS) { fprintf(stderr, "OCIHandleFree() - free connection pool handle error !\n"); return (-1); } } /* 釋放錯誤句柄 */ if (errhp != NULL) { rc = OCIHandleFree( errhp, OCI_HTYPE_ERROR ); if (rc != OCI_SUCCESS) { fprintf(stderr, "OCIHandleFree() - free error handle error !\n"); return (-1); } } /* 釋放OCI環境句柄 */ if (envhp != NULL) { rc = OCIHandleFree( envhp, OCI_HTYPE_ENV ); if (rc != OCI_SUCCESS) { fprintf(stderr, "OCIHandleFree() - free env handle error !\n"); return (-1); } } return (0); } /* 線程開始運行入口函數 */
static void thread_run(void *arg){ sword rc; sb4 ec; int num; int slen; ub4 u4; OCIError *errhp1 = NULL; OCISvcCtx *svchp1 = NULL; OCIStmt *stmthp = NULL; OCIDefine *defp; char sqltxt[1024]; OraText errbuf[512]; /* 創建本線程使用的錯誤句柄 */ rc = OCIHandleAlloc( (void *)envhp, (void **)&errhp1, OCI_HTYPE_ERROR, 0, (void **)NULL ); if (rc != OCI_SUCCESS) { fprintf(stderr, "OCIHandleAlloc() - allocate error handle error !\n"); return; } /* 獲取會話,使用了OCILogon2()函數 */ rc = OCILogon2(envhp, errhp1, &svchp1, (const OraText *)inputs.uname, (ub4)strlen(inputs.uname), (const OraText *)inputs.upwd, (ub4)strlen(inputs.upwd), (const OraText *)pool_name, (ub4)pool_name_len, OCI_CPOOL ); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp1, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCILogon2() - [%d] %s\n", ec, errbuf); goto thread_error; } /* 分配語句句柄 */ rc = OCIHandleAlloc( (void *)envhp, (void **)&stmthp, OCI_HTYPE_STMT, 0, (void **)NULL ); if (rc != OCI_SUCCESS) { fprintf(stderr, "OCIHandleAlloc() - allocate statement handle error !\n"); goto thread_error; } /* 得到線程傳入的參數 */ num = *((int *)arg); /* 使用會話從dual中獲取這個數字 */ sprintf(sqltxt, "select %d from dual", num); slen = strlen(sqltxt); /* 準備語句 */ rc = OCIStmtPrepare(stmthp, errhp1, (const OraText *)sqltxt, slen, OCI_NTV_SYNTAX, OCI_DEFAULT); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp1, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIStmtPrepare() - [%d] %s\n", ec, errbuf); goto thread_error; } /* 定義輸出變量 */ rc = OCIDefineByPos(stmthp, &defp, errhp1, (ub4)1, (void *)&u4, (sb4)4, (ub2)SQLT_INT, (void *)NULL, (ub2 *)NULL, (ub2 *)NULL, (ub4)OCI_DEFAULT ); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp1, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIDefineByPos() - [%d] %s\n", ec, errbuf); goto thread_error; } /* 執行查詢語句 */ rc = OCIStmtExecute(svchp1, stmthp, errhp1, 0, 0, NULL, NULL, OCI_DEFAULT); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp1, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIStmtExecute() - [%d] %s\n", ec, errbuf); goto thread_error; } /* 獲取結果集 */ rc = OCIStmtFetch2(stmthp, errhp1, 1, OCI_FETCH_NEXT, 0, OCI_DEFAULT); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp1, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIStmtFetch2() - [%d] %s\n", ec, errbuf); goto thread_error; } /* 打印取到的數字 */ fprintf(stdout, "number from dual is: %d\n", u4); /* 釋放語句句柄 */ rc = OCIHandleFree( stmthp, OCI_HTYPE_STMT ); if (rc != OCI_SUCCESS) { fprintf(stderr, "OCIHandleFree() - free statement handle error !\n"); goto thread_error; } stmthp = NULL; /* 退出會話 */ rc = OCILogoff(svchp1, errhp1); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp1, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCILogoff() - [%d] %s\n", ec, errbuf); goto thread_error; } svchp1 = NULL; /* 釋放錯誤句柄 */ rc = OCIHandleFree( errhp1, OCI_HTYPE_ERROR ); if (rc != OCI_SUCCESS) { fprintf(stderr, "OCIHandleFree() - free error handle error !\n"); } errhp1 = NULL; return; thread_error: if (stmthp != NULL) { OCIHandleFree(stmthp, OCI_HTYPE_STMT); } if (svchp1 != NULL) { OCILogoff(svchp1, errhp1); } if (errhp1 != NULL) { OCIHandleFree(errhp1, OCI_HTYPE_ERROR); } } /* 定義線程的最大個數,和傳入線程的數字數組 */
#define MAX_THREAD 4
int t_num[MAX_THREAD]; int main(int argc, char **argv){ int i; sword rc; sb4 ec; OCIThreadId *tid[MAX_THREAD]; OCIThreadHandle *thp[MAX_THREAD]; OraText errbuf[512]; /* 解析命令行參數 */ if (parse_inputs(argc, argv) < 0) return (-1); /* 檢查OCI程序運行的環境變量 */ if (check_ora_env() < 0) return (-1); /* 創建連接池 */ if (create_connection_pool() < 0) goto error_exit; /* 初始化OCI線程包 */ OCIThreadProcessInit(); /* 初始化線程包上下文 */ rc = OCIThreadInit(envhp, errhp); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIThreadInit() - [%d] %s\n", ec, errbuf); } /* 初始化每個線程的ID和句柄 */ for (i=0; i<MAX_THREAD; i++) { rc = OCIThreadIdInit(envhp, errhp, &tid[i]); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIThreadIdInit() - [%d] %s\n", ec, errbuf); } rc = OCIThreadHndInit(envhp, errhp, &thp[i]); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIThreadHndInit() - [%d] %s\n", ec, errbuf); } } /* 創建和啟動每個線程,傳入參數 */ for (i=0; i<MAX_THREAD; i++) { t_num[i] = i+1; rc = OCIThreadCreate(envhp, errhp, thread_run, (void *)&t_num[i], tid[i], thp[i]); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIThreadCreate() - [%d] %s\n", ec, errbuf); } } for (i=0; i<MAX_THREAD; i++) { /* 等待線程結束 */ rc = OCIThreadJoin(envhp, errhp, thp[i]); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIThreadJoin() - [%d] %s\n", ec, errbuf); } /* 關閉線程句柄 */ rc = OCIThreadClose(envhp, errhp, thp[i]); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIThreadClose() - [%d] %s\n", ec, errbuf); } /* 銷毀線程ID */ rc = OCIThreadIdDestroy(envhp, errhp, &tid[i]); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIThreadIdDestroy() - [%d] %s\n", ec, errbuf); } /* 銷毀線程句柄 */ rc = OCIThreadHndDestroy(envhp, errhp, &thp[i]); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIThreadHndDestroy() - [%d] %s\n", ec, errbuf); } } /* 結束線程包 */ rc = OCIThreadTerm(envhp, errhp); if (rc != OCI_SUCCESS) { OCIErrorGet(errhp, 1, NULL, &ec, errbuf, 512, OCI_HTYPE_ERROR); fprintf(stderr, "OCIThreadTerm() - [%d] %s\n", ec, errbuf); } /* 銷毀連接池 */ destroy_connection_pool(); return (0); error_exit: destroy_connection_pool(); return (-1); }
學習Oracle高級知識,編寫高質量代碼,盡在www.tomcoding.com

浙公網安備 33010602011771號