00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "ocilib_internal.h"
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 boolean OCI_PoolClose
00046 (
00047 OCI_Pool *pool
00048 )
00049 {
00050 boolean res = TRUE;
00051
00052 OCI_CHECK_PTR(OCI_IPC_POOL, pool, FALSE);
00053
00054
00055
00056 OCI_ListForEach(pool->cons, (POCI_LIST_FOR_EACH) OCI_ConnectionClose);
00057 OCI_ListClear(pool->cons);
00058 OCI_ListFree(pool->cons);
00059
00060 pool->cons = NULL;
00061
00062 if (OCI_LIB_THREADED)
00063 OCI_MutexFree(pool->mutex);
00064
00065 #if OCI_VERSION_COMPILE >= OCI_9_0
00066
00067 if (OCILib.version_runtime >= OCI_9_0)
00068 {
00069
00070
00071 if (pool->handle != NULL)
00072 {
00073 if (pool->htype == OCI_HTYPE_CPOOL)
00074 {
00075 OCI_CALL0
00076 (
00077 res, pool->err,
00078
00079 OCIConnectionPoolDestroy(pool->handle, pool->err,
00080 (ub4) OCI_DEFAULT)
00081 )
00082 }
00083
00084 #if OCI_VERSION_COMPILE >= OCI_9_2
00085
00086 else
00087 {
00088 OCI_CALL0
00089 (
00090 res, pool->err,
00091
00092 OCISessionPoolDestroy(pool->handle, pool->err,
00093 (ub4) OCI_SPD_FORCE)
00094 )
00095 }
00096
00097 #endif
00098
00099 OCI_HandleFree((void *) pool->handle, (ub4) pool->htype);
00100 }
00101
00102 #if OCI_VERSION_COMPILE >= OCI_9_2
00103
00104
00105
00106 if (pool->authp != NULL)
00107 OCI_HandleFree((void *) pool->authp, (ub4) OCI_HTYPE_AUTHINFO);
00108
00109 #endif
00110
00111
00112
00113 if (pool->err != NULL)
00114 OCI_HandleFree((void *) pool->err, (ub4) OCI_HTYPE_ERROR);
00115 }
00116
00117 #endif
00118
00119 pool->err = NULL;
00120 pool->handle = NULL;
00121 pool->authp = NULL;
00122
00123
00124
00125 OCI_FREE(pool->name);
00126 OCI_FREE(pool->db);
00127 OCI_FREE(pool->user);
00128 OCI_FREE(pool->pwd);
00129
00130 return res;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 OCI_Pool * OCI_API OCI_PoolCreate
00142 (
00143 const mtext *db,
00144 const mtext *user,
00145 const mtext *pwd,
00146 unsigned int type,
00147 unsigned int mode,
00148 unsigned int min_con,
00149 unsigned int max_con,
00150 unsigned int incr_con
00151 )
00152 {
00153 OCI_Pool *pool = NULL;
00154 OCI_Item *item = NULL;
00155 boolean res = TRUE;
00156
00157 OCI_CHECK_MIN(NULL, NULL, max_con, 1, NULL);
00158
00159
00160
00161 OCI_CHECK_INITIALIZED(NULL);
00162
00163
00164
00165 item = OCI_ListAppend(OCILib.pools, sizeof(*pool));
00166
00167 if (item != NULL)
00168 {
00169 pool = (OCI_Pool *) item->data;
00170
00171
00172
00173 pool->cons = OCI_ListCreate(OCI_IPC_CONNECTION);
00174
00175 if (OCI_LIB_THREADED)
00176 {
00177
00178
00179 pool->mutex = OCI_MutexCreateInternal();
00180
00181 res = (pool->mutex != NULL);
00182 }
00183 }
00184 else
00185 res = FALSE;
00186
00187
00188
00189 if (res == TRUE)
00190 {
00191
00192 pool->mode = mode;
00193 pool->min = min_con;
00194 pool->max = max_con;
00195 pool->incr = incr_con;
00196
00197 pool->db = mtsdup(db != NULL ? db : MT(""));
00198 pool->user = mtsdup(user != NULL ? user : MT(""));
00199 pool->pwd = mtsdup(pwd != NULL ? pwd : MT(""));
00200 }
00201
00202 #if OCI_VERSION_COMPILE < OCI_9_2
00203
00204 type = OCI_POOL_CONNECTION;
00205
00206 #endif
00207
00208 #if OCI_VERSION_COMPILE >= OCI_9_0
00209
00210 if (res == TRUE)
00211 {
00212 if (type == OCI_POOL_CONNECTION)
00213 pool->htype = OCI_HTYPE_CPOOL;
00214
00215 #if OCI_VERSION_COMPILE >= OCI_9_2
00216
00217 else
00218 pool->htype = OCI_HTYPE_SPOOL;
00219
00220 #endif
00221
00222 }
00223
00224 if (OCILib.version_runtime >= OCI_9_0)
00225 {
00226 int osize_name = -1;
00227 int osize_db = -1;
00228 int osize_user = -1;
00229 int osize_pwd = -1;
00230
00231 void *ostr_name = NULL;
00232 void *ostr_db = NULL;
00233 void *ostr_user = NULL;
00234 void *ostr_pwd = NULL;
00235
00236
00237
00238 if (res == TRUE)
00239 res = (OCI_SUCCESS == OCI_HandleAlloc((dvoid *) OCILib.env,
00240 (dvoid **) (void *) &pool->err,
00241 (ub4) OCI_HTYPE_ERROR,
00242 (size_t) 0,
00243 (dvoid **) NULL));
00244
00245
00246
00247 if (res == TRUE)
00248 res = (OCI_SUCCESS == OCI_HandleAlloc((dvoid *) OCILib.env,
00249 (dvoid **) (void *) &pool->handle,
00250 (ub4) pool->htype,
00251 (size_t) 0,
00252 (dvoid **) NULL));
00253
00254 #if OCI_VERSION_COMPILE >= OCI_9_2
00255
00256
00257
00258 if (res == TRUE)
00259 res = (OCI_SUCCESS == OCI_HandleAlloc((dvoid *) OCILib.env,
00260 (dvoid **) (void *) &pool->authp,
00261 (ub4) OCI_HTYPE_AUTHINFO,
00262 (size_t) 0,
00263 (dvoid **) NULL));
00264
00265 #endif
00266
00267
00268
00269 if (res == TRUE)
00270 {
00271 ostr_db = OCI_GetInputMetaString(pool->db, &osize_db);
00272 ostr_user = OCI_GetInputMetaString(pool->user, &osize_user);
00273 ostr_pwd = OCI_GetInputMetaString(pool->pwd, &osize_pwd);
00274
00275 if (pool->htype == OCI_HTYPE_CPOOL)
00276 {
00277 OCI_CALL3
00278 (
00279 res, pool->err,
00280
00281 OCIConnectionPoolCreate(OCILib.env, pool->err, pool->handle,
00282 (OraText **) (dvoid *) &ostr_name,
00283 (sb4*) &osize_name,
00284 (OraText *) ostr_db, (sb4) osize_db,
00285 (ub4) pool->min, (ub4) pool->max,
00286 (ub4) pool->incr, (OraText *) ostr_user,
00287 (sb4) osize_user, (OraText *) ostr_pwd,
00288 (sb4) osize_pwd, (ub4) OCI_DEFAULT)
00289 )
00290 }
00291
00292 #if OCI_VERSION_COMPILE >= OCI_9_2
00293
00294 else
00295 {
00296 OCI_CALL3
00297 (
00298 res, pool->err,
00299
00300 OCISessionPoolCreate(OCILib.env, pool->err, pool->handle,
00301 (OraText **) (dvoid *) &ostr_name,
00302 (ub4*) &osize_name,
00303 (OraText *) ostr_db, (sb4) osize_db,
00304 (ub4) pool->min, (ub4) pool->max,
00305 (ub4) pool->incr, (OraText *) ostr_user,
00306 (sb4) osize_user, (OraText *) ostr_pwd,
00307 (sb4) osize_pwd, (ub4) OCI_SPC_HOMOGENEOUS)
00308 )
00309 }
00310
00311 #endif
00312
00313 #if OCI_VERSION_COMPILE >= OCI_9_2
00314
00315
00316
00317 OCI_CALL3
00318 (
00319 res, pool->err,
00320
00321 OCIAttrSet((dvoid *) pool->authp,(ub4) OCI_HTYPE_AUTHINFO,
00322 (dvoid *) ostr_user, (ub4) osize_user,
00323 (ub4) OCI_ATTR_USERNAME, pool->err)
00324 )
00325
00326
00327
00328 OCI_CALL3
00329 (
00330 res, pool->err,
00331
00332 OCIAttrSet((dvoid *) pool->authp, (ub4) OCI_HTYPE_AUTHINFO,
00333 (dvoid *) ostr_pwd, (ub4) osize_pwd,
00334 (ub4) OCI_ATTR_PASSWORD, pool->err)
00335 )
00336
00337 #endif
00338
00339 OCI_ReleaseMetaString(ostr_db);
00340 OCI_ReleaseMetaString(ostr_user);
00341 OCI_ReleaseMetaString(ostr_pwd);
00342 }
00343
00344 if ((res == TRUE) && (ostr_name != NULL))
00345 {
00346 pool->name = (mtext *) OCI_MemAlloc(OCI_IPC_STRING, sizeof(mtext),
00347 (osize_name / (int) sizeof(omtext)) + 1,
00348 FALSE);
00349
00350 if (pool->name != NULL)
00351 {
00352 OCI_CopyString(ostr_name, pool->name, &osize_name,
00353 sizeof(omtext), sizeof(mtext));
00354 }
00355 else
00356 res = FALSE;
00357 }
00358 }
00359
00360 #endif
00361
00362
00363
00364
00365 if (res == TRUE)
00366 {
00367 OCI_Connection *cn;
00368
00369 while ((min_con--) > 0)
00370 {
00371 cn = OCI_ConnectionAllocate(pool, pool->db, pool->user,
00372 pool->pwd, pool->mode);
00373 }
00374 }
00375 else
00376 {
00377 OCI_PoolFree(pool);
00378 pool = NULL;
00379 }
00380
00381 OCI_RESULT(res);
00382
00383 return pool;
00384 }
00385
00386
00387
00388
00389
00390 boolean OCI_API OCI_PoolFree
00391 (
00392 OCI_Pool *pool
00393 )
00394 {
00395 boolean res = TRUE;
00396
00397 OCI_CHECK_PTR(OCI_IPC_POOL, pool, FALSE);
00398
00399 res = OCI_PoolClose(pool);
00400
00401 OCI_ListRemove(OCILib.pools, pool);
00402
00403 OCI_FREE(pool);
00404
00405 OCI_RESULT(res);
00406
00407 return res;
00408 }
00409
00410
00411
00412
00413
00414 OCI_Connection * OCI_API OCI_PoolGetConnection
00415 (
00416 OCI_Pool *pool,
00417 mtext *tag
00418 )
00419 {
00420 OCI_Connection *con = NULL;
00421 OCI_Item *item = NULL;
00422 boolean res = FALSE;
00423 boolean found = FALSE;
00424
00425 OCI_CHECK_PTR(OCI_IPC_POOL, pool, NULL);
00426
00427 if (OCI_LIB_THREADED)
00428 OCI_MutexAcquire(pool->mutex);
00429
00430
00431
00432 item = pool->cons->head;
00433
00434 while (item != NULL)
00435 {
00436 con = (OCI_Connection *) item->data;
00437
00438 if (((OCILib.version_runtime >= OCI_9_0) && (con->cstate == OCI_CONN_ALLOCATED)) ||
00439 ((OCILib.version_runtime < OCI_9_0) && (con->cstate == OCI_CONN_ATTACHED )))
00440 {
00441 found = TRUE;
00442 break;
00443 }
00444
00445 item = item->next;
00446 }
00447
00448 if (found == FALSE)
00449 {
00450 con = NULL;
00451
00452
00453
00454 if (OCILib.version_runtime >= OCI_9_0 || pool->cons->count < pool->max)
00455 {
00456 ub4 i, nb;
00457 OCI_Connection *c = NULL;
00458
00459 nb = pool->nb_opened + pool->incr;
00460
00461 if (nb > pool->max)
00462 nb = pool->max;
00463
00464 for (i = pool->nb_opened; i < nb; i++)
00465 {
00466 c = OCI_ConnectionAllocate(pool, pool->db, pool->user,
00467 pool->pwd, pool->mode);
00468
00469 if (i == pool->nb_opened && c != NULL)
00470 con = c;
00471 }
00472 }
00473 }
00474
00475 if (con != NULL)
00476 {
00477 res = TRUE;
00478
00479 if (con->cstate == OCI_CONN_ALLOCATED)
00480 res = res && OCI_ConnectionAttach(con);
00481
00482 res = res && OCI_ConnectionLogon(con, NULL, tag);
00483
00484 if (res == FALSE)
00485 {
00486 OCI_ConnectionFree(con);
00487 con = NULL;
00488 }
00489 }
00490 else
00491 {
00492 con = NULL;
00493 }
00494
00495 if (OCI_LIB_THREADED)
00496 OCI_MutexRelease(pool->mutex);
00497
00498 OCI_RESULT(res);
00499
00500 return con;
00501 }
00502
00503
00504
00505
00506
00507 unsigned int OCI_API OCI_PoolGetTimeout
00508 (
00509 OCI_Pool *pool
00510 )
00511 {
00512 OCI_CHECK_PTR(OCI_IPC_POOL, pool, 0);
00513
00514 OCI_RESULT(TRUE);
00515
00516 return pool->timeout;
00517 }
00518
00519
00520
00521
00522
00523 boolean OCI_API OCI_PoolSetTimeout
00524 (
00525 OCI_Pool *pool,
00526 unsigned int value
00527 )
00528 {
00529 boolean res = TRUE;
00530
00531 OCI_CHECK_PTR(OCI_IPC_POOL, pool, FALSE);
00532
00533 #if OCI_VERSION_COMPILE >= OCI_9_0
00534
00535 if (OCILib.version_runtime >= OCI_9_0)
00536 {
00537 ub4 timeout = value;
00538 ub4 attr = 0;
00539
00540 if (pool->htype == OCI_HTYPE_CPOOL)
00541 attr = OCI_ATTR_CONN_TIMEOUT ;
00542
00543 #if OCI_VERSION_COMPILE >= OCI_9_2
00544
00545 else
00546 attr = OCI_ATTR_SPOOL_TIMEOUT;
00547 #endif
00548
00549 OCI_CALL3
00550 (
00551 res, pool->err,
00552
00553 OCIAttrSet((dvoid *) pool->handle, (ub4) pool->htype,
00554 (dvoid *) &timeout,(ub4) sizeof(timeout),
00555 (ub4) attr, pool->err)
00556 )
00557 }
00558
00559 #endif
00560
00561 if (res == TRUE)
00562 pool->timeout = value;
00563
00564 OCI_RESULT(res);
00565
00566 return res;
00567 }
00568
00569
00570
00571
00572
00573 boolean OCI_API OCI_PoolGetNoWait
00574 (
00575 OCI_Pool *pool
00576 )
00577 {
00578 OCI_CHECK_PTR(OCI_IPC_POOL, pool, FALSE);
00579
00580 OCI_RESULT(TRUE);
00581
00582 return pool->nowait;
00583 }
00584
00585
00586
00587
00588
00589 boolean OCI_API OCI_PoolSetNoWait
00590 (
00591 OCI_Pool *pool,
00592 boolean value
00593 )
00594 {
00595 boolean res = TRUE;
00596
00597 OCI_CHECK_PTR(OCI_IPC_POOL, pool, 0);
00598
00599 #if OCI_VERSION_COMPILE >= OCI_9_0
00600
00601 if (OCILib.version_runtime >= OCI_9_0)
00602 {
00603 ub1 nowait = (ub1) value;
00604 ub4 attr = 0;
00605
00606 if (pool->htype == OCI_HTYPE_CPOOL)
00607 attr = OCI_ATTR_CONN_NOWAIT;
00608
00609 #if OCI_VERSION_COMPILE >= OCI_9_2
00610
00611 else
00612 {
00613 attr = OCI_ATTR_SPOOL_GETMODE;
00614
00615 if (value == TRUE)
00616 nowait = (ub1) OCI_SPOOL_ATTRVAL_NOWAIT;
00617 else
00618 nowait = (ub1) OCI_SPOOL_ATTRVAL_WAIT;
00619 }
00620
00621 #endif
00622
00623 OCI_CALL3
00624 (
00625 res, pool->err,
00626
00627 OCIAttrSet((dvoid *) pool->handle, (ub4) pool->htype,
00628 (dvoid *) &nowait, (ub4) sizeof(nowait),
00629 (ub4) attr, pool->err)
00630 )
00631 }
00632
00633 #endif
00634
00635 if (res == TRUE)
00636 pool->nowait = value;
00637
00638 OCI_RESULT(res);
00639
00640 return TRUE;
00641 }
00642
00643
00644
00645
00646
00647 unsigned int OCI_API OCI_PoolGetBusyCount
00648 (
00649 OCI_Pool *pool
00650 )
00651 {
00652 boolean res = TRUE;
00653
00654 OCI_CHECK_PTR(OCI_IPC_POOL, pool, 0);
00655
00656 #if OCI_VERSION_COMPILE >= OCI_9_0
00657
00658 if (OCILib.version_runtime >= OCI_9_0)
00659 {
00660 ub4 value = 0;
00661 ub4 attr = 0;
00662
00663 if (pool->htype == OCI_HTYPE_CPOOL)
00664 attr = (ub4) OCI_ATTR_CONN_BUSY_COUNT;
00665
00666 #if OCI_VERSION_COMPILE >= OCI_9_2
00667
00668 else
00669 attr = (ub4) OCI_ATTR_SPOOL_BUSY_COUNT;
00670
00671 #endif
00672
00673 OCI_CALL3
00674 (
00675 res, pool->err,
00676
00677 OCIAttrGet((dvoid *) pool->handle,(ub4) pool->htype,
00678 (dvoid *) &value, (ub4 *) NULL,
00679 (ub4) attr, pool->err)
00680 )
00681
00682 if (res == TRUE)
00683 pool->nb_busy = value;
00684 }
00685
00686 #endif
00687
00688 OCI_RESULT(res);
00689
00690 return pool->nb_busy;
00691 }
00692
00693
00694
00695
00696
00697 unsigned int OCI_API OCI_PoolGetOpenedCount
00698 (
00699 OCI_Pool *pool
00700 )
00701 {
00702 boolean res = TRUE;
00703
00704 OCI_CHECK_PTR(OCI_IPC_POOL, pool, 0);
00705
00706 #if OCI_VERSION_COMPILE >= OCI_9_0
00707
00708 if (OCILib.version_runtime >= OCI_9_0)
00709 {
00710 ub4 value = 0;
00711 ub4 attr = 0;
00712
00713 if (pool->htype == OCI_HTYPE_CPOOL)
00714 attr = OCI_ATTR_CONN_OPEN_COUNT;
00715
00716 #if OCI_VERSION_COMPILE >= OCI_9_2
00717
00718 else
00719 attr = OCI_ATTR_SPOOL_OPEN_COUNT;
00720
00721 #endif
00722
00723 OCI_CALL3
00724 (
00725 res, pool->err,
00726
00727 OCIAttrGet((dvoid *) pool->handle, (ub4) pool->htype,
00728 (dvoid *) &value, (ub4 *) NULL,
00729 (ub4) attr, pool->err)
00730 )
00731
00732 if (res == TRUE)
00733 pool->nb_opened = value;
00734 }
00735
00736 #endif
00737
00738 OCI_RESULT(res);
00739
00740 return pool->nb_opened;
00741 }
00742
00743
00744
00745
00746
00747 unsigned int OCI_API OCI_PoolGetMin
00748 (
00749 OCI_Pool *pool
00750 )
00751 {
00752 OCI_CHECK_PTR(OCI_IPC_POOL, pool, 0);
00753
00754 OCI_RESULT(TRUE);
00755
00756 return pool->min;
00757 }
00758
00759
00760
00761
00762
00763 unsigned int OCI_API OCI_PoolGetMax
00764 (
00765 OCI_Pool *pool
00766 )
00767 {
00768 OCI_CHECK_PTR(OCI_IPC_POOL, pool, 0);
00769
00770 OCI_RESULT(TRUE);
00771
00772 return pool->max;
00773 }
00774
00775
00776
00777
00778
00779 unsigned int OCI_API OCI_PoolGetIncrement
00780 (
00781 OCI_Pool *pool
00782 )
00783 {
00784 OCI_CHECK_PTR(OCI_IPC_POOL, pool, 0);
00785
00786 OCI_RESULT(TRUE);
00787
00788 return pool->incr;
00789 }