forked from gwenshap/DBD-Oracle
-
Notifications
You must be signed in to change notification settings - Fork 25
/
dbdcnx.c
1103 lines (1053 loc) · 31.4 KB
/
dbdcnx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* notes {{{
* This module attempts to provide reuse of OCIEnv
* and storage for session pool(s).
*
* Note, configuration uses acronym "drcp", which is
* somewhat misleading. DRCP stands for "connection pooling"
* and DBD::Oracle offers "session pooling", which is
* not "connection pooling" (that is also offered by Oracle).
*
* To make things complex DBD::Oracle offers 2 ways to
* share connections between threads. Such sharing
* requires that one thread does not use pointers
* to data allocated in another thread. In perl,
* each thread gets copy of perl interpreter, and each
* thread has its own heap for memory allocation.
* When the thread ends, that memory may get released,
* which leads to problems if the memory is referenced
* from another thread.
*
* It appears, that OCI uses itsown memory pool for
* allocation, so the data allocated in one thread
* is still accessible in another thread. At least
* tests don't fail in such cases. There are
* also OCI internal mutexes that protect access to
* that memory. The good news, one may ask OCI to allocate
* some memory for the caller. That is what this
* module is using.
*
* When OCIEnv is created, it uses 3 parameters
* that define the functionality of new OCIEnv.
* Those are: mode, charset and ncharset. So, when
* we need some OCIEnv we have to check for existing
* ones that have matching parameters. If charset
* or ncharset is 0, then current value from environment
* is used (OCINlsEnvironmentVariableGet)
*
* The session pool is identified (in addition to
* charsets and mode for OCIEnv) by dbname, user
* and "rlb" (activity of load-balancer). Since all
* sessions are "homogenous", Oracle won't check
* password when new session is requested. So probably
* it makes sense to check it here. So password
* is also saved.
*
* This module does not use HV to find cached
* handles because HV may need to allocate memory
* from current thread, and that is not good. Still
* it is very unlikely that someone uses thousands
* of different pools/characters, so simple
* running through doubly-linked list should be
* fast enough (and maybe even faster) than hashing.
*
* Now, how long shall be cached all handles? As long
* as there are users. Each open connection is a user.
* Additionally every "driver"-object is a user.
* The drivier-object is allocated in each thread
* that tries to connect to DB and is released when
* thread ends.
}}} */
#include "Oracle.h"
DBISTATE_DECLARE;
typedef struct llist_t llist_t;
/* small implementation for doubly-linked list {{{ */
struct llist_t{
llist_t * left;
llist_t * right;
};
#define llist_empty(list) ((list)->left == (list))
#define llist_init(lst) do{\
llist_t * list = lst;\
list->right = list->left = list;\
}while(0)
#define llist_add(aleft, aright) do{\
llist_t * old;\
llist_t * left = aleft;\
llist_t * right = aright;\
old = left->right;\
left->right = right;\
old->left = right->left;\
right->left = left;\
old->left->right = old;\
}while(0)
#define llist_drop(ael) do{\
llist_t * el = ael;\
if(!llist_empty(el)) {\
el->left->right = el->right;\
el->right->left = el->left;\
llist_init(el);\
} \
}while(0)
// this is pointer to the left element in chain
#define llist_left(list) (list)->left
// this one is a pointer to the right element in chain
#define llist_right(list) (list)->right
/* }}} */
#if defined(USE_ITHREADS)
static perl_mutex mng_lock;
#endif
static llist_t mng_list;
static int dr_instances;
/* to get information about charsets we need these */
static OCIEnv * mng_env;
static OCIError * mng_err;
/*dbd_dr_globals_init{{{*/
void
dbd_dr_globals_init()
{
#if defined(USE_ITHREADS)
dTHX;
MUTEX_INIT(&mng_lock);
#endif
llist_init(&mng_list);
dr_instances = 0;
mng_env = NULL;
mng_err = NULL;
}
/*}}}*/
struct box_st{
llist_t lock;
int refs; /* this shall be positiv for OCIEnv and negativ for Session Pool */
};
struct env_box_st
{
box_t base;
OCIEnv * envhp;
ub4 mode;
ub2 cset;
ub2 ncset;
};
typedef struct env_box_st env_box_t;
#ifdef ORA_OCI_112
struct pool_box_st{
box_t base;
env_box_t * env;
OCISPool *poolhp;
OCIError *errhp;
OraText *name;
ub4 name_len;
ub2 pass_len;
ub2 dbname_len;
/* all text strings are stored below. The buffer
* is long enough to contain dbname_len+user_len+pass_len + 4 bytes.
* So pass is box->buf + 1;
* dbname is box->buf + 2 + box->pass_len
* user is box->buf + 3 + box->dbname_len + box->pass_len
* First byte is 0 if RLB is not desired.
*/
char buf[1];
};
typedef struct pool_box_st pool_box_t;
#endif
#define tracer(imp, dlvl, vlvl, ...) if \
(DBIc_DBISTATE(imp)->debug >= (dlvl) || dbd_verbose >= (vlvl) )\
PerlIO_printf(DBIc_LOGPIO(imp), __VA_ARGS__)
/* local_error{{{*/
static int
local_error(pTHX_ SV * h, const char * fmt, ...)
{
va_list ap;
SV * txt_sv = sv_newmortal();
SV * code_sv = get_sv("DBI::stderr", 0);
D_imp_xxh(h);
if(code_sv == NULL)
{
code_sv = sv_newmortal();
sv_setiv(code_sv, 2000000000);
}
va_start(ap, fmt);
sv_vsetpvf(txt_sv, fmt, &ap);
va_end(ap);
DBIh_SET_ERR_SV(h, imp_xxh, code_sv, txt_sv, &PL_sv_undef, &PL_sv_undef);
return FALSE;
}
/*}}}*/
/* release_env{{{*/
static void
release_env(pTHX_ env_box_t * box)
{
llist_drop(&box->base.lock);
if(dbd_verbose >= 3)
warn("Releasing OCIEnv %p\n", box->envhp);
(void)OCIHandleFree(box->envhp, OCI_HTYPE_ENV);
}/*}}}*/
/* new_envhp_box{{{*/
static int
new_envhp_box(pTHX_ env_box_t ** slot, dblogin_info_t * ctrl)
{
imp_dbh_t * imp_dbh = ctrl->imp_dbh;
env_box_t * box;
OCIEnv * envhp;
sword status = OCIEnvNlsCreate(
&envhp, ctrl->mode,
0, NULL, NULL, NULL,
sizeof(*box), (dvoid**)&box,
imp_dbh->cset, imp_dbh->ncset
);
if (status != OCI_SUCCESS)
return local_error(aTHX_ ctrl->dbh, "Failed to allocate OCIEnv");
tracer(imp_dbh, 3, 3, "allocated new OCIEnv %p (cset %d, ncset %d, mode %d)\n",
envhp, (int)imp_dbh->cset, (int)imp_dbh->ncset, (int)ctrl->mode);
llist_init(&box->base.lock);
box->envhp = envhp;
box->base.refs = dr_instances;
box->cset = imp_dbh->cset;
box->ncset = imp_dbh->ncset;
box->mode = ctrl->mode;
llist_add(&mng_list, &box->base.lock);
*slot = box;
return TRUE;
}/*}}}*/
/* figure_out_charsets {{{*/
static int
figure_out_charsets(pTHX_ dblogin_info_t * ctrl)
{
imp_dbh_t * imp_dbh = ctrl->imp_dbh;
if(ctrl->cset != NULL)
{
imp_dbh->cset = OCINlsCharSetNameToId(
mng_env, (OraText*)ctrl->cset
);
if(imp_dbh->cset == 0) return local_error(
aTHX_ ctrl->dbh, "Invalid ora_charset '%s'", ctrl->cset
);
}
else if(imp_dbh->cset == 0)
{
size_t rsize;
sword status = OCINlsEnvironmentVariableGet(
&imp_dbh->cset, 0, OCI_NLS_CHARSET_ID, 0, &rsize
);
if(status != OCI_SUCCESS || imp_dbh->cset == 0)
return local_error(aTHX_ ctrl->dbh, "NLS_LANG appears to be invalid");
}
if(ctrl->ncset != NULL)
{
imp_dbh->ncset = OCINlsCharSetNameToId(
mng_env, (OraText*)ctrl->ncset
);
if(imp_dbh->ncset == 0) return local_error(
aTHX_ ctrl->dbh, "Invalid ora_ncharset '%s'", ctrl->ncset
);
}
else if(imp_dbh->ncset == 0)
{
size_t rsize;
sword status = OCINlsEnvironmentVariableGet(
&imp_dbh->ncset, 0, OCI_NLS_NCHARSET_ID, 0, &rsize
);
if(status != OCI_SUCCESS || imp_dbh->ncset == 0)
return local_error(aTHX_ ctrl->dbh, "NLS_NCHAR appears to be invalid");
}
return TRUE;
}/*}}}*/
/*find_env{{{*/
static env_box_t *
find_env(ub4 mode, ub2 cset, ub2 ncset)
{
llist_t * base = llist_left(&mng_list);
while(base != &mng_list)
{
env_box_t * box = (env_box_t *)base;
if(box->base.refs > 0
&& box->mode == mode
&& box->ncset == ncset
&& box->cset == cset
)
{
/* check if this handle is still valid */
if(box->base.refs == dr_instances)
{
OCIError *errhp;
sword status=OCIHandleAlloc(
box->envhp,
(dvoid**)&errhp,
OCI_HTYPE_ERROR, 0, NULL
);
if(status != OCI_SUCCESS)
{
llist_drop(base);
OCIHandleFree(box->envhp, OCI_HTYPE_ENV);
return NULL;
}
}
return box;
}
base = llist_left(base);
}
return NULL;
}
/*}}}*/
/* simple_connect {{{*/
static int
simple_connect(pTHX_ dblogin_info_t * ctrl)
{
imp_dbh_t * imp_dbh = ctrl->imp_dbh;
sword status;
ub4 ulen, plen, ctype;
tracer(imp_dbh, 3, 3, "using OCIEnv %p to connect\n", imp_dbh->envhp);
OCIHandleAlloc_ok(
imp_dbh, imp_dbh->envhp, &imp_dbh->errhp,
OCI_HTYPE_ERROR, status
);
if(status != OCI_SUCCESS)
return local_error(aTHX_ ctrl->dbh, "Failed to allocate OCIError\n");
OCIHandleAlloc_ok(
imp_dbh, imp_dbh->envhp, &imp_dbh->srvhp,
OCI_HTYPE_SERVER, status
);
if(status != OCI_SUCCESS)
return local_error(aTHX_ ctrl->dbh, "Failed to allocate OCIServer\n");
OCIHandleAlloc_ok(
imp_dbh, imp_dbh->envhp, &imp_dbh->svchp,
OCI_HTYPE_SVCCTX, status
);
if(status != OCI_SUCCESS)
return local_error(aTHX_ ctrl->dbh, "Failed to allocate OCISvcCtx\n");
OCIHandleAlloc_ok(
imp_dbh, imp_dbh->envhp, &imp_dbh->seshp,
OCI_HTYPE_SESSION, status
);
if(status != OCI_SUCCESS)
return local_error(aTHX_ ctrl->dbh, "Failed to allocate OCISession\n");
OCIServerAttach_log_stat(imp_dbh, ctrl->dbname,OCI_DEFAULT, status);
if (status != OCI_SUCCESS)
return oci_error(ctrl->dbh, imp_dbh->errhp, status, "OCIServerAttach");
OCIAttrSet_log_stat(
imp_dbh, imp_dbh->svchp,
OCI_HTYPE_SVCCTX,
imp_dbh->srvhp,
(ub4) 0, OCI_ATTR_SERVER, imp_dbh->errhp, status
);
if (status != OCI_SUCCESS) return oci_error(
ctrl->dbh, imp_dbh->errhp, status, "OCIAttrSet OCI_HTYPE_SVCCTX"
);
plen = (ub4)strlen(ctrl->pwd);
ulen = (ub4)strlen(ctrl->uid);
if(plen == 0 && ulen == 0) ctype = OCI_CRED_EXT;
else
{
ctype = OCI_CRED_RDBMS;
OCIAttrSet_log_stat(
imp_dbh, imp_dbh->seshp,
OCI_HTYPE_SESSION,
ctrl->uid, ulen,
(ub4) OCI_ATTR_USERNAME,
imp_dbh->errhp, status
);
if (status != OCI_SUCCESS) return oci_error(
ctrl->dbh, imp_dbh->errhp, status, "OCIAttrSet OCI_ATTR_USERNAME"
);
OCIAttrSet_log_stat(
imp_dbh, imp_dbh->seshp,
OCI_HTYPE_SESSION,
((plen) ? ctrl->pwd : NULL), plen,
(ub4) OCI_ATTR_PASSWORD,
imp_dbh->errhp, status
);
if (status != OCI_SUCCESS) return oci_error(
ctrl->dbh, imp_dbh->errhp, status, "OCIAttrSet OCI_ATTR_PASSWORD"
);
}
#ifdef ORA_OCI_112
if(ctrl->driver_name_len != 0)
{
OCIAttrSet_log_stat(
imp_dbh, imp_dbh->seshp, OCI_HTYPE_SESSION,
ctrl->driver_name, ctrl->driver_name_len,
OCI_ATTR_DRIVER_NAME,
imp_dbh->errhp, status
);
if (status != OCI_SUCCESS) return oci_error(
ctrl->dbh, imp_dbh->errhp, status, "OCIAttrSet OCI_ATTR_DRIVER_NAME"
);
}
#endif
OCISessionBegin_log_stat(
imp_dbh, imp_dbh->svchp, imp_dbh->errhp, imp_dbh->seshp,
ctype, ctrl->session_mode, status
);
if (status == OCI_SUCCESS_WITH_INFO)
{
/* eg ORA-28011: the account will expire soon; change your password now */
oci_error(ctrl->dbh, imp_dbh->errhp, status, "OCISessionBegin");
status = OCI_SUCCESS;
}
if (status != OCI_SUCCESS)
return oci_error(ctrl->dbh, imp_dbh->errhp, status, "OCISessionBegin");
OCIAttrSet_log_stat(
imp_dbh, imp_dbh->svchp,
(ub4) OCI_HTYPE_SVCCTX,
imp_dbh->seshp, (ub4) 0,
(ub4) OCI_ATTR_SESSION,
imp_dbh->errhp, status
);
if (status != OCI_SUCCESS) return oci_error(
ctrl->dbh, imp_dbh->errhp, status, "OCIAttrSet OCI_ATTR_SESSION"
);
return TRUE;
}/*}}}*/
#ifdef ORA_OCI_112
/*release_pool{{{*/
static void
release_pool(pTHX_ pool_box_t * box)
{
env_box_t * ebox;
if(dbd_verbose >= 3)
warn("Releasing pool %p\n", box->poolhp);
ebox = box->env;
llist_drop(&box->base.lock);
(void)OCISessionPoolDestroy (box->poolhp, box->errhp, OCI_DEFAULT);
(void)OCIHandleFree(box->poolhp, OCI_HTYPE_SPOOL);
ebox->base.refs--;
if(ebox->base.refs == 0) release_env(aTHX_ ebox);
}/*}}}*/
/*cnx_pool_min{{{*/
void
cnx_pool_min(pTHX_ SV * dbh, imp_dbh_t * imp_dbh, ub4 val)
{
pool_box_t * box;
sword status;
if(imp_dbh->lock->refs > 0) return;
box = ((pool_box_t*)imp_dbh->lock);
status = OCIAttrSet(
box->poolhp, OCI_HTYPE_SPOOL,
(dvoid*)&val, sizeof(val),
OCI_ATTR_SPOOL_MIN, box->errhp
);
if(status != OCI_SUCCESS)
(void)oci_error(dbh, box->errhp, status, "OCIAttrSet POOL_MIN");
}/*}}}*/
/*cnx_pool_max{{{*/
void
cnx_pool_max(pTHX_ SV * dbh, imp_dbh_t * imp_dbh, ub4 val)
{
pool_box_t * box;
sword status;
if(imp_dbh->lock->refs > 0) return;
box = ((pool_box_t*)imp_dbh->lock);
status = OCIAttrSet(
box->poolhp, OCI_HTYPE_SPOOL,
(dvoid*)&val, sizeof(val),
OCI_ATTR_SPOOL_MAX, box->errhp
);
if(status != OCI_SUCCESS)
(void)oci_error(dbh, box->errhp, status, "OCIAttrSet POOL_MAX");
}/*}}}*/
/*cnx_pool_incr{{{*/
void
cnx_pool_incr(pTHX_ SV * dbh, imp_dbh_t * imp_dbh, ub4 val)
{
pool_box_t * box;
sword status;
if(imp_dbh->lock->refs > 0) return;
box = ((pool_box_t*)imp_dbh->lock);
status = OCIAttrSet(
box->poolhp, OCI_HTYPE_SPOOL,
(dvoid*)&val, sizeof(val),
OCI_ATTR_SPOOL_INCR, box->errhp
);
if(status != OCI_SUCCESS)
(void)oci_error(dbh, box->errhp, status, "OCIAttrSet POOL_INCR");
}/*}}}*/
/*cnx_pool_mode{{{*/
void
cnx_pool_mode(pTHX_ SV * dbh, imp_dbh_t * imp_dbh, ub4 val)
{
pool_box_t * box;
sword status;
if(imp_dbh->lock->refs > 0) return;
box = ((pool_box_t*)imp_dbh->lock);
status = OCIAttrSet(
box->poolhp, OCI_HTYPE_SPOOL,
(dvoid*)&val, sizeof(val),
OCI_ATTR_SPOOL_GETMODE, box->errhp
);
if(status != OCI_SUCCESS)
(void)oci_error(dbh, box->errhp, status, "OCIAttrSet POOL_GETMODE");
}/*}}}*/
/*cnx_pool_wait{{{*/
#if OCI_MAJOR_VERSION > 18
void
cnx_pool_wait(pTHX_ SV * dbh, imp_dbh_t * imp_dbh, ub4 val)
{
pool_box_t * box;
sword status;
if(imp_dbh->lock->refs > 0) return;
box = ((pool_box_t*)imp_dbh->lock);
status = OCIAttrSet(
box->poolhp, OCI_HTYPE_SPOOL,
(dvoid*)&val, sizeof(val),
OCI_ATTR_SPOOL_WAIT_TIMEOUT, box->errhp
);
if(status != OCI_SUCCESS)
(void)oci_error(dbh, box->errhp, status, "OCIAttrSet POOL_WAIT_TIMEOUT");
}
#endif
/*}}}*/
/*cnx_is_pooled_session{{{*/
int
cnx_is_pooled_session(pTHX_ SV *dbh, imp_dbh_t * imp_dbh)
{
return (imp_dbh->lock->refs < 0);
}/*}}}*/
/*cnx_get_pool_mode{{{*/
int
cnx_get_pool_mode(pTHX_ SV *dbh, imp_dbh_t * imp_dbh)
{
pool_box_t * box;
ub4 v, l;
sword status;
if(imp_dbh->lock->refs > 0) return 0;
box = ((pool_box_t*)imp_dbh->lock);
status = OCIAttrGet(
box->poolhp, OCI_HTYPE_SPOOL,
(dvoid*)&v, &l,
OCI_ATTR_SPOOL_GETMODE, box->errhp
);
if(status == OCI_SUCCESS) return (int)v;
(void)oci_error(dbh, box->errhp, status, "OCIAttrGet POOL_METHOD");
return 0;
}/*}}}*/
/*cnx_get_pool_wait{{{*/
#if OCI_MAJOR_VERSION > 18
int
cnx_get_pool_wait(pTHX_ SV *dbh, imp_dbh_t * imp_dbh)
{
pool_box_t * box;
ub4 v, l;
sword status;
if(imp_dbh->lock->refs > 0) return 0;
box = ((pool_box_t*)imp_dbh->lock);
status = OCIAttrGet(
box->poolhp, OCI_HTYPE_SPOOL,
(dvoid*)&v, &l,
OCI_ATTR_SPOOL_WAIT_TIMEOUT, box->errhp
);
if(status == OCI_SUCCESS) return (int)v;
(void)oci_error(dbh, box->errhp, status, "OCIAttrGet POOL_WAIT_TIMEOUT");
return 0;
}
#endif
/*}}}*/
/*cnx_get_pool_used{{{*/
int
cnx_get_pool_used(pTHX_ SV *dbh, imp_dbh_t * imp_dbh)
{
pool_box_t * box;
ub4 v, l;
sword status;
if(imp_dbh->lock->refs > 0) return 0;
box = ((pool_box_t*)imp_dbh->lock);
status = OCIAttrGet(
box->poolhp, OCI_HTYPE_SPOOL,
(dvoid*)&v, &l,
OCI_ATTR_SPOOL_BUSY_COUNT, box->errhp
);
if(status == OCI_SUCCESS) return (int)v;
(void)oci_error(dbh, box->errhp, status, "OCIAttrGet POOL_USED");
return 0;
}/*}}}*/
/*cnx_get_pool_max{{{*/
int
cnx_get_pool_max(pTHX_ SV *dbh, imp_dbh_t * imp_dbh)
{
pool_box_t * box;
ub4 v, l;
sword status;
if(imp_dbh->lock->refs > 0) return 0;
box = ((pool_box_t*)imp_dbh->lock);
status = OCIAttrGet(
box->poolhp, OCI_HTYPE_SPOOL,
(dvoid*)&v, &l,
OCI_ATTR_SPOOL_MAX, box->errhp
);
if(status == OCI_SUCCESS) return (int)v;
(void)oci_error(dbh, box->errhp, status, "OCIAttrGet POOL_MAX");
return 0;
}/*}}}*/
/*cnx_get_pool_min{{{*/
int
cnx_get_pool_min(pTHX_ SV *dbh, imp_dbh_t * imp_dbh)
{
pool_box_t * box;
ub4 v, l;
sword status;
if(imp_dbh->lock->refs > 0) return 0;
box = ((pool_box_t*)imp_dbh->lock);
status = OCIAttrGet(
box->poolhp, OCI_HTYPE_SPOOL,
(dvoid*)&v, &l,
OCI_ATTR_SPOOL_MIN, box->errhp
);
if(status == OCI_SUCCESS) return (int)v;
(void)oci_error(dbh, box->errhp, status, "OCIAttrGet POOL_MIN");
return 0;
}/*}}}*/
/*cnx_get_pool_incr{{{*/
int
cnx_get_pool_incr(pTHX_ SV *dbh, imp_dbh_t * imp_dbh)
{
pool_box_t * box;
ub4 v, l;
sword status;
if(imp_dbh->lock->refs > 0) return 0;
box = ((pool_box_t*)imp_dbh->lock);
status = OCIAttrGet(
box->poolhp, OCI_HTYPE_SPOOL,
(dvoid*)&v, &l,
OCI_ATTR_SPOOL_INCR, box->errhp
);
if(status == OCI_SUCCESS) return (int)v;
(void)oci_error(dbh, box->errhp, status, "OCIAttrGet POOL_INCR");
return 0;
}/*}}}*/
/*cnx_get_pool_rlb{{{*/
int
cnx_get_pool_rlb(pTHX_ SV *dbh, imp_dbh_t * imp_dbh)
{
if(imp_dbh->lock->refs > 0) return 0;
return (int)(((pool_box_t*)imp_dbh->lock)->buf[0]);
}/*}}}*/
/*find_pool{{{*/
static pool_box_t *
find_pool(dblogin_info_t * ctrl)
{
imp_dbh_t * imp_dbh = ctrl->imp_dbh;
llist_t * base = llist_left(&mng_list);
while(base != &mng_list)
{
char * name;
pool_box_t * box = (pool_box_t *)base;
base = llist_left(base);
if(box->base.refs > 0) continue;
if(box->env->mode != ctrl->mode) continue;
if(box->env->ncset != imp_dbh->ncset) continue;
if(box->env->cset != imp_dbh->cset) continue;
name = box->buf + 2 + box->pass_len;
if(0 != strcmp(name, ctrl->dbname)) continue;
name += box->dbname_len + 1;
if(0 != strcmp(name, ctrl->uid)) continue;
if((int)(box->buf[0]) != (int)(ctrl->pool_rlb)) continue;
return box;
}
return NULL;
}
/*}}}*/
/* new_pool_box{{{*/
int
new_pool_box(pTHX_ pool_box_t ** slot, dblogin_info_t * ctrl)
{
imp_dbh_t * imp_dbh = ctrl->imp_dbh;
OCIEnv * envhp = imp_dbh->envhp;
pool_box_t * box;
char * name;
OCISPool *poolhp;
ub4 mode;
ub4 dbname_len = strlen(ctrl->dbname);
ub4 user_len = strlen(ctrl->uid);
ub4 pwd_len = strlen(ctrl->pwd);
size_t boxlen = sizeof(*box) + dbname_len + user_len + pwd_len + 3;
sword status = OCIHandleAlloc(
envhp, (dvoid*)&poolhp, OCI_HTYPE_SPOOL, boxlen, (dvoid**)&box
);
if (status != OCI_SUCCESS)
return local_error(aTHX_ ctrl->dbh, "Failed to allocate OCISPool");
status = OCIHandleAlloc(envhp, (dvoid*)&box->errhp, OCI_HTYPE_ERROR, 0, NULL);
if (status != OCI_SUCCESS)
{
OCIHandleFree(poolhp, OCI_HTYPE_SPOOL);
return local_error(aTHX_ ctrl->dbh, "Failed to allocate OCIError");
}
#ifdef ORA_OCI_112
if(ctrl->driver_name_len != 0)
{
OCIAuthInfo * authp;
status = OCIHandleAlloc(envhp, (dvoid*)&authp, OCI_HTYPE_AUTHINFO, 0, NULL);
if(status != OCI_SUCCESS)
{
OCIHandleFree(box->errhp, OCI_HTYPE_ERROR);
OCIHandleFree(poolhp, OCI_HTYPE_SPOOL);
return local_error(aTHX_ ctrl->dbh, "Failed to allocate OCIAuthInfo");
}
status = OCIAttrSet(
authp, OCI_HTYPE_AUTHINFO,
(OraText*)ctrl->driver_name, ctrl->driver_name_len,
OCI_ATTR_DRIVER_NAME,
box->errhp
);
if(status != OCI_SUCCESS)
{
(void)oci_error(ctrl->dbh, box->errhp, status, "OCIAttrSet DriverName");
OCIHandleFree(box->errhp, OCI_HTYPE_ERROR);
OCIHandleFree(authp, OCI_HTYPE_AUTHINFO);
OCIHandleFree(poolhp, OCI_HTYPE_SPOOL);
return FALSE;
}
status = OCIAttrSet(
poolhp, OCI_HTYPE_SPOOL,
authp, 0,
OCI_ATTR_SPOOL_AUTH,
box->errhp
);
if(status != OCI_SUCCESS)
{
(void)oci_error(ctrl->dbh, box->errhp, status, "OCIAttrSet OCIAuthInfo");
OCIHandleFree(box->errhp, OCI_HTYPE_ERROR);
OCIHandleFree(authp, OCI_HTYPE_AUTHINFO);
OCIHandleFree(poolhp, OCI_HTYPE_SPOOL);
return FALSE;
}
OCIHandleFree(authp, OCI_HTYPE_AUTHINFO);
}
#endif
mode = OCI_SPC_HOMOGENEOUS;
if(!ctrl->pool_rlb) mode |= OCI_SPC_NO_RLB;
status = OCISessionPoolCreate(
envhp,
box->errhp,
poolhp,
&box->name,
&box->name_len,
(OraText *) ctrl->dbname,
dbname_len,
ctrl->pool_min,
ctrl->pool_max,
ctrl->pool_incr,
(OraText *) ctrl->uid,
user_len,
(OraText *) ctrl->pwd,
pwd_len,
mode
);
if (status != OCI_SUCCESS)
{
(void)oci_error(ctrl->dbh, box->errhp, status, "OCISessionPoolCreate");
OCIHandleFree(box->errhp, OCI_HTYPE_ERROR);
OCIHandleFree(poolhp, OCI_HTYPE_SPOOL);
return FALSE;
}
llist_init(&box->base.lock);
box->env = (env_box_t *)imp_dbh->lock;
box->buf[0] = ctrl->pool_rlb;
name = box->buf + 1;
strcpy(name, ctrl->pwd);
box->pass_len = pwd_len;
name += pwd_len + 1;
strcpy(name, ctrl->dbname);
box->dbname_len = dbname_len;
name += dbname_len + 1;
strcpy(name, ctrl->uid);
box->base.refs = -dr_instances;
llist_add(&mng_list, &box->base.lock);
box->poolhp = poolhp;
*slot = box;
return TRUE;
}/*}}}*/
/* session_from_pool {{{*/
static int
session_from_pool(pTHX_ dblogin_info_t * ctrl)
{
imp_dbh_t * imp_dbh = ctrl->imp_dbh;
sword status;
OCIAuthInfo *authp;
OraText *rettag;
OraText *tag;
ub4 rettagl;
STRLEN tagl;
boolean session_tag_found;
/* try to find existing pool */
pool_box_t * box = find_pool(ctrl);
if(box != NULL)
{
if(0 != strcmp(box->buf + 1, ctrl->pwd))
return local_error(aTHX_ ctrl->dbh, "Password for session is wrong");
}
else if(!new_pool_box(aTHX_ &box, ctrl)) return FALSE;
/* replace env-box with pool-box. It shall be cleared by DESTROY */
imp_dbh->lock = &box->base;
box->base.refs--; /* refcount for pool is negative! I know, I know... */
OCIHandleAlloc_ok(
imp_dbh, imp_dbh->envhp, &imp_dbh->errhp, OCI_HTYPE_ERROR, status
);
if(status != OCI_SUCCESS)
return local_error(aTHX_ ctrl->dbh, "OCIError alloc failed");
OCIHandleAlloc_ok(
imp_dbh, imp_dbh->envhp, &authp, OCI_HTYPE_AUTHINFO, status
);
if(status != OCI_SUCCESS)
return local_error(aTHX_ ctrl->dbh, "OCIAuthInfo alloc failed");
if(ctrl->pool_class != NULL)
{
tag = (OraText*)SvPV(ctrl->pool_class, tagl);
OCIAttrSet_log_stat(
imp_dbh, authp,
OCI_HTYPE_AUTHINFO,
tag, (ub4) tagl,
OCI_ATTR_CONNECTION_CLASS, imp_dbh->errhp, status
);
if(status != OCI_SUCCESS) return local_error(aTHX_ ctrl->dbh,
"OCIAuthInfo setting connection class failed");
}
tag = NULL;
tagl = 0;
if(ctrl->pool_tag != NULL)
{
tag = (OraText*)SvPV(ctrl->pool_tag, tagl);
if(tagl == 0) tag = NULL;
}
OCISessionGet_log_stat(
imp_dbh, imp_dbh->envhp,
imp_dbh->errhp,
&imp_dbh->svchp,
authp,
box->name, box->name_len,
tag, (ub4)tagl,
&rettag, &rettagl, &session_tag_found,
status
);
if (status != OCI_SUCCESS)
{
(void)oci_error(ctrl->dbh, imp_dbh->errhp, status, "OCISessionGet");
OCIHandleFree_log_stat(imp_dbh, authp, OCI_HTYPE_AUTHINFO, status);
return FALSE;
}
if(session_tag_found && rettagl != 0)
{
if(imp_dbh->session_tag != NULL) SvREFCNT_dec(imp_dbh->session_tag);
imp_dbh->session_tag = newSVpv((char*)rettag, rettagl);
}
OCIHandleFree_log_stat(imp_dbh, authp, OCI_HTYPE_AUTHINFO, status);
/* Get server and session handles from service context handle,
* allocated by OCISessionGet. */
OCIAttrGet_log_stat(
imp_dbh, imp_dbh->svchp, OCI_HTYPE_SVCCTX, &imp_dbh->srvhp, NULL,
OCI_ATTR_SERVER, imp_dbh->errhp, status
);
if(status != OCI_SUCCESS)
return oci_error(ctrl->dbh, imp_dbh->errhp, status, "Server get");
OCIAttrGet_log_stat(
imp_dbh, imp_dbh->svchp, OCI_HTYPE_SVCCTX, &imp_dbh->seshp, NULL,
OCI_ATTR_SESSION, imp_dbh->errhp, status
);
if(status != OCI_SUCCESS)
return oci_error(ctrl->dbh, imp_dbh->errhp, status, "Session get");
return 1;
}
/*}}}*/
#endif
#if defined(USE_ITHREADS)
static int _cnx_establish
#else
int cnx_establish
#endif
(pTHX_ dblogin_info_t * ctrl)
{
imp_dbh_t * imp_dbh = ctrl->imp_dbh;
env_box_t * env_box;
if(!figure_out_charsets(aTHX_ ctrl)) return FALSE;
/* try to find existing OCIEnv */
env_box = find_env(ctrl->mode, imp_dbh->cset, imp_dbh->ncset);
if(env_box == NULL && !new_envhp_box(aTHX_ &env_box, ctrl)) return FALSE;
env_box->base.refs++;
imp_dbh->envhp = env_box->envhp;
imp_dbh->lock = &env_box->base;
imp_dbh->errhp = NULL;
imp_dbh->srvhp = NULL;
imp_dbh->svchp = NULL;
imp_dbh->seshp = NULL;
/* Now I want DESTROY to be called if something goes wrong */
DBIc_IMPSET_on(imp_dbh);
#ifdef ORA_OCI_112
if(ctrl->pool_max != 0)
return session_from_pool(aTHX_ ctrl);
#endif
return simple_connect(aTHX_ ctrl);
}
#if defined(USE_ITHREADS)
int
cnx_establish(pTHX_ dblogin_info_t * ctrl)
{
int rv;
MUTEX_LOCK(&mng_lock);
rv = _cnx_establish(aTHX_ ctrl);
MUTEX_UNLOCK(&mng_lock);
return rv;
}
#endif
/*dbd_dr_mng{{{
* this function is called every time new DR object is created.
* It is responsible for incrementing refs on all cached objects
*/
void
dbd_dr_mng()
{
llist_t * el;
#if defined(USE_ITHREADS)
dTHX;
MUTEX_LOCK(&mng_lock);
#endif
dr_instances++;
el = llist_left(&mng_list);
while(el != &mng_list)
{
box_t * base = (box_t *)el;
#ifdef ORA_OCI_112
if(base->refs < 0) base->refs--;
else
#endif
base->refs++;
el = llist_left(el);
}
if(mng_env == NULL)
{
sword status = OCIEnvNlsCreate(
&mng_env, OCI_DEFAULT,
0, NULL, NULL, NULL,
0, NULL, 0, 0
);
if(status == OCI_SUCCESS)
status=OCIHandleAlloc(
mng_env,
(dvoid**)&mng_err,
OCI_HTYPE_ERROR,
0, NULL
);
utf8_csid = OCINlsCharSetNameToId(mng_env, (void*)"UTF8");
al32utf8_csid = OCINlsCharSetNameToId(mng_env, (void*)"AL32UTF8");
al16utf16_csid = OCINlsCharSetNameToId(mng_env, (void*)"AL16UTF16");
}
#if defined(USE_ITHREADS)
MUTEX_UNLOCK(&mng_lock);
#endif
}
/*}}}*/
/* cnx_detach{{{*/
void
cnx_detach(pTHX_ imp_dbh_t * imp_dbh)
{
/* Oracle will commit on an orderly disconnect. */
/* See DBI Driver.xst file for the DBI approach. */
#ifdef ORA_OCI_112
if (imp_dbh->lock->refs < 0)
{
/* Release session, tagged for future retrieval. */
if(imp_dbh->session_tag != NULL)
{
STRLEN tlen;
char * tag = SvPV(imp_dbh->session_tag, tlen);
(void)OCISessionRelease(
imp_dbh->svchp, imp_dbh->errhp,
(OraText*)tag, (ub4)tlen, OCI_SESSRLS_RETAG
);
SvREFCNT_dec(imp_dbh->session_tag);