Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.tools -> Re: OCI with a Multithreading App

Re: OCI with a Multithreading App

From: laurent <laurent.citton_at_socgen.com>
Date: 2000/06/19
Message-ID: <394DDBDE.2E9A38BE@socgen.com>

Hi David,

We use Oracle OCI 8.1.6 to multiplex multiple sessions on a single connection in a multithreaded environment. You will find hereafter an extraction of the code we wrote to achieve that. This WON'T WORK in OCI 8.0.x release, altough Oracle says it should....

Hope it will help you.

 /* Initialize OCI environment */
(void) OCIInitialize((ub4) OCI_THREADED, (dvoid *)0, NULL, NULL, NULL );

  /* Initialize connection to server */
(void) OCIEnvInit( (OCIEnv **) &envhp, OCI_DEFAULT, (size_t) 0, (dvoid **)
0 );
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &errhp,
OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0);
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &srvhp,
OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0);
(void) OCIServerAttach( srvhp, errhp, SERVER, strlen((char*)SERVER), 0);

 /* Initialize first session */
(void) OCIEnvInit( (OCIEnv **) &FirstSessionEnvhp, OCI_DEFAULT, (size_t)
0, (dvoid **) 0 );
(void) OCIHandleAlloc( (dvoid *) FirstSessionEnvhp, (dvoid **)
&FirstSessionErrhp, OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0);
(void) OCIHandleAlloc( (dvoid *) FirstSessionEnvhp, (dvoid **)
&FirstSessionCtxhp, OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0);
(void) OCIHandleAlloc((dvoid *) FirstSessionEnvhp, (dvoid
**)&FirstSessionAuthp, (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0);   checkerr(FirstSessionErrhp, OCIAttrSet((dvoid *) FirstSessionAuthp, (ub4) OCI_HTYPE_SESSION,

                                         (dvoid *) USERNAME,
                                         (ub4) strlen((char *)USERNAME),
                                         (ub4) OCI_ATTR_USERNAME,
FirstSessionErrhp));
  checkerr(FirstSessionErrhp, OCIAttrSet((dvoid *) FirstSessionAuthp, (ub4) OCI_HTYPE_SESSION,
                                         (dvoid *) PASSWORD,
                                         (ub4) strlen((char *)PASSWORD),
                                         (ub4) OCI_ATTR_PASSWORD,
FirstSessionErrhp));
  checkerr(FirstSessionErrhp, OCIAttrSet((dvoid *) FirstSessionCtxhp, (ub4) OCI_HTYPE_SVCCTX,
                                         (dvoid *) FirstSessionAuthp, (ub4)
0,
                                         (ub4) OCI_ATTR_SESSION,
FirstSessionErrhp));
  checkerr(FirstSessionErrhp, OCIAttrSet( (dvoid *) FirstSessionCtxhp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp,
                                          (ub4) 0, OCI_ATTR_SERVER,
(OCIError *) FirstSessionErrhp));
  checkerr(FirstSessionErrhp, OCISessionBegin ( FirstSessionCtxhp, FirstSessionErrhp, FirstSessionAuthp, OCI_CRED_RDBMS,
                                                (ub4) OCI_DEFAULT));


  /* Initialize second session */
(void) OCIEnvInit( (OCIEnv **) &SecondSessionEnvhp, OCI_DEFAULT, (size_t)
0, (dvoid **) 0 );
(void) OCIHandleAlloc( (dvoid *) SecondSessionEnvhp, (dvoid **)
&SecondSessionErrhp, OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0);
(void) OCIHandleAlloc( (dvoid *) SecondSessionEnvhp, (dvoid **)
&SecondSessionCtxhp, OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0);
(void) OCIHandleAlloc((dvoid *) SecondSessionEnvhp, (dvoid
**)&SecondSessionAuthp, (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0);   checkerr(SecondSessionErrhp, OCIAttrSet((dvoid *) SecondSessionAuthp, (ub4) OCI_HTYPE_SESSION,

                                          (dvoid *) USERNAME,
                                          (ub4) strlen((char*)USERNAME),
                                          (ub4) OCI_ATTR_USERNAME,
SecondSessionErrhp));
  checkerr(SecondSessionErrhp, OCIAttrSet((dvoid *) SecondSessionAuthp, (ub4) OCI_HTYPE_SESSION,
                                          (dvoid *) PASSWORD,
                                          (ub4) strlen((char*)PASSWORD),
                                          (ub4) OCI_ATTR_PASSWORD,
SecondSessionErrhp));
  checkerr(SecondSessionErrhp, OCIAttrSet((dvoid *) SecondSessionCtxhp, (ub4) OCI_HTYPE_SVCCTX,
                                          (dvoid *) SecondSessionAuthp,
(ub4) 0,
                                          (ub4) OCI_ATTR_SESSION,
SecondSessionErrhp));
  checkerr(SecondSessionErrhp, OCIAttrSet( (dvoid *) SecondSessionCtxhp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp,
                                           (ub4) 0, OCI_ATTR_SERVER,
(OCIError *) SecondSessionErrhp));
  checkerr(SecondSessionErrhp, OCISessionBegin ( SecondSessionCtxhp, SecondSessionErrhp, SecondSessionAuthp, OCI_CRED_RDBMS,
                                                 (ub4) OCI_DEFAULT));






David Murphy wrote:

> Hi - I am trying to figure out how to initialize OCI so that threads in
> a multithreaded app do not block each other.
>
> The app creates threads T1, T2 both of which connect to the Oracle
> database to perform SQL.
> T1 connects and starts processing SQL insert and selects. T2 starts and
> connects OK - however at this
> point T2 tries to process its SQL statements but ends up blocked,
> waiting for T1. When T1 finsishes T2 starts it
> processsing.
>
> So the question is how to allow T1 and T2 to operate without blocking.
> Both T1 and T2 have created their own
> OCI Environment handle and login under that handle. The handle is
> created with OCIEnvCreate with OCI_DEFAULT
> as the mode. They also have their own error and service context handles.
>
> When I tried OCI_THREADED as the mode for the OCIEnvCreate I get a
> access violation when disconnecting T1.
> Its not clear from the OCI doc whether I need to specify OCI_THREADED
> as the mode
> in OCIEnvCreated - anyone know about this?
>
> I also tried to have T1 and T2 share the same OCI Environment handle.
> With OCI_DEFAULT T1 blocked T2.
> With OCI_THREADED I get the same access violation.
>
> The Ora doc is on this is confusing to say the least.. Any help much
> appreciated.
>
> Regards
> David Murphy
> NetDirect_at_compuserve.com
>
> The sequence of calls for each thread is ..
>
> OCIEnvCreate(&OCIEnvHandle, OCI_DEFAULT /*OCI_THREADED*/ ,
>                           NULL, NULL, NULL, NULL, 0, NULL);
> OCIHandleAlloc((dvoid *) OCIEnvHandle,
>       (dvoid **) &OCIErrorHandle,
>       (ub4) OCI_HTYPE_ERROR,
>       (size_t) 0, (dvoid **) 0);
>
> OCILogon((OCIEnv *) OCIEnvHandle, OCIErrorHandle, &OCISvcHandle, ..)
>
> OCIHandleAlloc((dvoid *)pConnection->OCIEnvHandle, (dvoid **)
> &OCIStatementHandle,
>                            (ub4)OCI_HTYPE_STMT, (CONST size_t) 0,
> (dvoid **) 0);
>
> OCIStmtExecute(pConnection->OCISvcHandle,
>                              OCIStatementHandle,
>                              pConnection->OCIErrorHandle,
>                              (ub4) nIterations,
>                              (ub4) 0,
>                              (OCISnapshot *) NULL,
>                              (OCISnapshot *) NULL,
>                              (ub4) OCI_DEFAULT);
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
Received on Mon Jun 19 2000 - 00:00:00 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US