<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      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); }

       

       

       

      posted @ 2025-08-14 22:09  湯姆花花  閱讀(6)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 亚洲成人av免费一区| 亚洲岛国成人免费av| 国产AV无码专区亚洲AV紧身裤 | 国产乱码精品一品二品| 久热这里只有精品视频3| 97亚洲熟妇自偷自拍另类图片| 欧美国产成人精品二区芒果视频| 免费观看欧美猛交视频黑人| 国产精品国产精品一区精品| 亚洲精品在线二区三区| 国产偷自视频区视频| 国产精品自在线拍国产手机版 | 久久久av波多野一区二区| 久久夜色精品国产噜噜亚洲sv| 亚洲真人无码永久在线| 国产中文99视频在线观看| 99精品国产一区二区三区不卡| 亚洲午夜香蕉久久精品| 天天拍夜夜添久久精品大| 欧美色aⅴ欧美综合色| 国产学生裸体无遮挡免费| 日韩亚洲精品中文字幕| 精品久久久久久无码人妻蜜桃| 亚洲AV高清一区二区三区尤物| 亚洲av乱码一区二区| 国产精品久久久久孕妇| 亚洲爆乳WWW无码专区| 精品无码国产污污污免费| 国产高清在线男人的天堂| 99国产精品久久久久久久日本竹 | 亚洲欧美日韩综合一区二区| 九九热免费在线观看视频| 精品亚洲一区二区三区四区| 中日韩精品视频一区二区三区 | 亚洲熟女乱色一区二区三区| 免青青草免费观看视频在线 | 麻豆精品一区二区视频在线| 四虎影视一区二区精品| 无套中出极品少妇白浆| 亚洲精品中文字幕一区二| 国产成人毛片无码视频软件|