ocisession::ocisession(const char * connect_str, const int connect_str_len, const char * user_name, const int user_name_len, const char * password, const int password_len) { intf_ret r; OCIAuthInfo *authp = NULL; init(); r.handle = envhp; // allocate error handle checkenv(&r, OCIHandleAlloc((OCIEnv*)envhp, /* environment handle */ (void **) &_errhp, /* returned err handle */ OCI_HTYPE_ERROR, /* typ of handle to allocate */ (size_t) 0, /* optional extra memory size */ (void **) NULL)); /* returned extra memeory */ if(r.fn_ret != SUCCESS) { REMOTE_LOG("failed OCISessionGet %s\n", r.gerrbuf); throw r; } // allocate auth handle checkenv(&r, OCIHandleAlloc((OCIEnv*)envhp, (void**)&authp, OCI_HTYPE_AUTHINFO, (size_t)0, (void **) NULL)); r.handle = _errhp; // usrname and password checkerr(&r, OCIAttrSet(authp, OCI_HTYPE_AUTHINFO, (void*) user_name, user_name_len, OCI_ATTR_USERNAME, (OCIError *)_errhp)); checkerr(&r, OCIAttrSet(authp, OCI_HTYPE_AUTHINFO, (void*) password, password_len, OCI_ATTR_PASSWORD, (OCIError *)_errhp)); /* get the database connection */ checkerr(&r, OCISessionGet((OCIEnv*)envhp, (OCIError *)_errhp, (OCISvcCtx**)&_svchp, /* returned database connection */ authp, /* initialized authentication handle */ (OraText *) connect_str, connect_str_len,/* connect string */ NULL, 0, NULL, NULL, NULL, /* session tagging parameters: optional */ OCI_SESSGET_STMTCACHE)); /* modes */ if(r.fn_ret != SUCCESS) { REMOTE_LOG("failed OCISessionGet %s\n", r.gerrbuf); throw r; } (void) OCIHandleFree(authp, OCI_HTYPE_AUTHINFO); REMOTE_LOG("got session %p\n", _svchp); _sessions.push_back(this); }
/* thread_function can be run in multi-threads, each thread will have its own error handle */ void thread_function(void *ptr) { OCIError *errhp = NULL; int i=0; /* allocate error handle * note: for OCIHandleAlloc(), we always check error on environment handle */ checkenv(envhp, OCIHandleAlloc(envhp, /* environment handle */ (void **) &errhp, /* returned err handle */ OCI_HTYPE_ERROR,/*type of handle to allocate*/ (size_t) 0, /* extra memory size: optional */ (void **) NULL)); /* returned extra memory */ for(i=0; i<iteration; i++) { OCISvcCtx *svchp = NULL; /* OCI Service Context is a database connection */ /* get the database connection */ checkerr(errhp, OCISessionGet(envhp, errhp, &svchp, /* returned database connection */ authp, /* initialized authentication handle */ /* connect pool name */ (OraText *) poolName, poolNameLen, /* session tagging parameters: optional */ NULL, 0, NULL, NULL, NULL, OCI_SESSGET_SPOOL)); do_workload(svchp, errhp, ptr,i); /* destroy the connection */ checkerr(errhp, OCISessionRelease(svchp, errhp, NULL, 0, OCI_DEFAULT)); print_progress(ptr, i); /* print progress */ if (waittime > 0) sleep(waittime); /* Thinking time between database sessions */ } if (errhp) OCIHandleFree(errhp, OCI_HTYPE_ERROR); }
void * oci_get_connection_from_pool() { function_success = SUCCESS; /* get the database connection */ OCISvcCtx *svchp = NULL; checkerr(errhp, OCISessionGet(envhp, errhp, &svchp, /* returned database connection */ NULL, /* initialized authentication handle */ /* connect string */ (OraText *) poolName, poolNameLen, /* session tagging parameters: optional */ NULL, 0, NULL, NULL, NULL, OCI_SESSGET_SPOOL));/* modes */ if(function_success != SUCCESS) return NULL; return svchp; }