2015년 4월 29일 수요일

OCI connection pool 사용하기




----

#include "ocilib.h"

#define MAX_DATE_LEN   20
#define MAX_THREADS 50
#define MAX_CONN    10

void worker(OCI_Thread *thread, void *data)
{
    char sz_date[MAX_DATE_LEN+1];
    OCI_Connection *p_cn = OCI_PoolGetConnection(data, NULL);

    /* application work here */
    sz_date[0] = 0;

    OCI_Immediate(p_cn, "select to_char(sysdate, 'YYYYMMDD HH24:MI:SS') from dual", OCI_ARG_TEXT, sz_date);
   
    printf("%s\n", sz_date);
   
    /* ... */
    OCI_ConnectionFree(p_cn);
}

int main(void)
{
    int i;
    OCI_ConnPool *p_conn_pool;
    OCI_Thread *p_thread[MAX_THREADS];

    if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT | OCI_ENV_THREADED))
        return EXIT_FAILURE;

    /* create connection pool */
    p_conn_pool = OCI_PoolCreate("db", "usr", "pwd", OCI_POOL_CONNECTION, OCI_SESSION_DEFAULT, 0, MAX_CONN, 1);

    /* create threads */
    for (i = 0; i < MAX_THREADS; i++) {
        p_thread[i] = OCI_ThreadCreate();
        OCI_ThreadRun(p_thread[i], worker, p_conn_pool);
    }
 
    /* wait for threads and free */
    for (i = 0; i < MAX_THREADS; i++) {
       OCI_ThreadJoin(p_thread[i]);
       OCI_ThreadFree(p_thread[i]);
    }

    /* free connection pool and OCI cleanup */
    OCI_PoolFree(p_conn_pool);
    OCI_Cleanup();

    return EXIT_SUCCESS;
}

댓글 없음:

댓글 쓰기