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

Home -> Community -> Mailing Lists -> Oracle-L -> RE: Race condition in Oracle C Interface?

RE: Race condition in Oracle C Interface?

From: Mohan, Ross <MohanR_at_STARS-SMI.com>
Date: Mon, 20 Nov 2000 10:31:00 -0500
Message-Id: <10686.122501@fatcity.com>


This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible.

------_=_NextPart_001_01C05306.6ED15320

Content-Type: text/plain;

        charset="iso-8859-1"

The client locks up you believe?

It would be interesting to run
it under Un*x, just to see if the
same thing happens.

Could be an NT threading thing, there
are some performance issues with older
versions of NT and small thread scheduling...

just a thought

-----Original Message-----

From: Michael Haggerty [mailto:mhagger_at_alum.mit.edu] Sent: Sunday, November 19, 2000 6:00 PM
To: Multiple recipients of list ORACLE-L Subject: Race condition in Oracle C Interface?

I am trying to use Oracle in a multithreaded application that creates and destroys multiple database connections. It seems to have tickled a bug in Oracle, though, because the application hangs up deep in Oracle. I simplified the application into the trivial C program appended below, which on my system locks up about half of the time. It just opens up two database connections and some associated structures and then closes them. I don't think there is a problem with code, but if you are sceptical just take a look; it is quite trivial.

Has anybody seen this problem? Does anyone know a workaround?

I am running Oracle 8.1.6.1 under Linux/Intel with kernel version 2.2.14. The program locks up within OCIServerDetach and has to be kill -6'ed or stronger. Only the client seems to lock up; the server seems unaffected.

Thanks,
Michael

--

Michael Haggerty
mhagger_at_alum.mit.edu

/* ==================== main.c ======================== */

/* compile with:

    gcc -I/ora8/m01/app/oracle/product/8.1.6/rdbms/demo

-I/ora8/m01/app/oracle/product/8.1.6/network/public
-I/ora8/m01/app/oracle/product/8.1.6/plsql/public
-I/ora8/m01/app/oracle/product/8.1.6/rdbms/public -DORACLE8i -g -o main
-L/ora8/m01/app/oracle/product/8.1.6/lib/ -lclntsh
-Wl,-rpath,/ora8/m01/app/oracle/product/8.1.6/lib ./main.c

*/

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <oci.h>


typedef struct {
	OCIEnv		*envhp;			/* Environment handle 	*/
	OCIError	*errhp;			/* Error handle		*/
	OCIServer	*srvhp;			/* Server handle	*/
	OCISession	*usrhp;			/* User handle		*/
	OCISvcCtx	*svchp;			/* Service Context	*/
} ServerContext;

ServerContext *Connect(char *user, char *password, char *database) {

	ServerContext *sc;
	int	userlen;
	int	passwordlen;
	int	databaselen;
	sword	status;
	sword	credentials = OCI_CRED_RDBMS;

        userlen = strlen(user);
        passwordlen = strlen(password);
        databaselen = strlen(database);

        sc = (ServerContext *)malloc(sizeof(ServerContext));

	sc->envhp = NULL;
	sc->errhp = NULL;
	sc->svchp = NULL;
	sc->srvhp = NULL;
	sc->usrhp = NULL;

	status = OCIEnvCreate(&sc->envhp, OCI_THREADED, NULL, NULL, NULL,
NULL,
                              0, NULL);
	assert(status == OCI_SUCCESS);

	/* Allocate the error handle */
	status = OCIHandleAlloc(sc->envhp, (dvoid **)&sc->errhp,
                                OCI_HTYPE_ERROR, 0, NULL);
	assert(status == OCI_SUCCESS);

	/* Allocate the server handle */
	status = OCIHandleAlloc(sc->envhp, (dvoid **) &sc->srvhp,
                                OCI_HTYPE_SERVER, 0, NULL);
	assert(status == OCI_SUCCESS);

	/* Connect to database */
	status = OCIServerAttach(sc->srvhp, sc->errhp, database,
databaselen,
                                 OCI_DEFAULT);
	assert(status == OCI_SUCCESS);

	/* Allocate a service context */
	status = OCIHandleAlloc(sc->envhp, (dvoid **) &sc->svchp,
                                OCI_HTYPE_SVCCTX, 0, NULL);
	assert(status == OCI_SUCCESS);

	/* Attach the server to the service context */
	status = OCIAttrSet(sc->svchp, OCI_HTYPE_SVCCTX, (dvoid *)
sc->srvhp,
                            (ub4) 0, OCI_ATTR_SERVER, sc->errhp);
	assert(status == OCI_SUCCESS);

	/* Allocate a session */
	status = OCIHandleAlloc(sc->envhp, (dvoid **) &sc->usrhp,
                                OCI_HTYPE_SESSION, 0, NULL);
	assert(status == OCI_SUCCESS);

	/* Were credentials provided? if so, set them in the session */
        status = OCIAttrSet(sc->usrhp, OCI_HTYPE_SESSION,
                            (dvoid *) user, (ub4) userlen,
                            OCI_ATTR_USERNAME, sc->errhp);
        assert(status == OCI_SUCCESS);

        status = OCIAttrSet(sc->usrhp, OCI_HTYPE_SESSION,
                            (dvoid *) password, (ub4) passwordlen,
                            OCI_ATTR_PASSWORD, sc->errhp);
        assert(status == OCI_SUCCESS);
	
	/* Now begin the session */
	status = OCISessionBegin(sc->svchp, sc->errhp, sc->usrhp,
                                 credentials, OCI_DEFAULT);
	assert(status == OCI_SUCCESS);

	/* Now attach the session to the service context */
	status = OCIAttrSet(sc->svchp, OCI_HTYPE_SVCCTX,
                            sc->usrhp, 0, OCI_ATTR_SESSION, sc->errhp);
	assert(status == OCI_SUCCESS);

	return sc;

}
/*
** ServerContext_dealloc
**
** Called when a server context goes out of scope; hang up the connection
** to the database!

*/

void ServerContext_dealloc(ServerContext *self) {

        sword status;

	/* End the session */
	assert(self->svchp);
        status = OCISessionEnd(self->svchp, self->errhp, self->usrhp,
                               OCI_DEFAULT);
        assert(status == OCI_SUCCESS);

	/* Disconnect from the server */
	assert(self->srvhp);
        status = OCIServerDetach(self->srvhp, self->errhp, OCI_DEFAULT);
        assert(status == OCI_SUCCESS);

	/* Deallocate the session handle */
        assert(self->usrhp);
        status = OCIHandleFree(self->usrhp, OCI_HTYPE_SESSION);
        assert(status == OCI_SUCCESS);

	/* Deallocate the server handle */
        assert(self->srvhp);
        status = OCIHandleFree(self->srvhp, OCI_HTYPE_SERVER);
        assert(status == OCI_SUCCESS);

	/* Deallocate the service context handle */
	assert(self->svchp);
        status = OCIHandleFree(self->svchp, OCI_HTYPE_SVCCTX);
        assert(status == OCI_SUCCESS);

	/* Deallocate the error handle */
        assert(self->errhp);
        status = OCIHandleFree(self->errhp, OCI_HTYPE_ERROR);
        assert(status == OCI_SUCCESS);

	/* Deallocate the environment handle */
        assert(self->envhp);
        status = OCIHandleFree(self->envhp, OCI_HTYPE_ENV);
        assert(status == OCI_SUCCESS);

        free(self);

}

int main(int argc, char *argv[]) {

    char *user;
    char *password;
    char *database;

    ServerContext *db1;
    ServerContext *db2;
    assert(argc == 4);
    user = argv[1];
    password = argv[2];
    database = argv[3];
    db1 = Connect(user, password, database);     db2 = Connect(user, password, database);     fprintf(stderr, "Deleting db2.\n");
    ServerContext_dealloc(db2);
    fprintf(stderr, "Deleting db1.\n");
    ServerContext_dealloc(db1);
    fprintf(stderr, "This line often not reached.\n");     return 0;
}

--

Please see the official ORACLE-L FAQ: http://www.orafaq.com
--

Author: Michael Haggerty
  INET: mhagger_at_alum.mit.edu

Fat City Network Services    -- (858) 538-5051  FAX: (858) 538-5051
San Diego, California        -- Public Internet access / Mailing Lists

--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).

------_=_NextPart_001_01C05306.6ED15320

Content-Type: text/html;

        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version = 5.5.2650.12">
<TITLE>RE: Race condition in Oracle C Interface?</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>The client locks up you believe?</FONT> </P>

<P><FONT SIZE=3D2>It would be interesting to run</FONT>
<BR><FONT SIZE=3D2>it under Un*x, just to see if the</FONT>
<BR><FONT SIZE=3D2>same thing happens. </FONT>
</P>

<P><FONT SIZE=3D2>Could be an NT threading thing, there</FONT>
<BR><FONT SIZE=3D2>are some performance issues with older</FONT>
<BR><FONT SIZE=3D2>versions of NT and small thread scheduling...</FONT> </P>

<P><FONT SIZE=3D2>just a thought</FONT>
</P>

<P><FONT SIZE=3D2>- Ross Mohan</FONT>
</P>

<P><FONT SIZE=3D2>-----Original Message-----</FONT> <BR><FONT SIZE=3D2>From: Michael Haggerty [<A = HREF=3D"mailto:mhagger_at_alum.mit.edu">mailto:mhagger_at_alum.mit.edu</A>]</F=

ONT>
<BR><FONT SIZE=3D2>Sent: Sunday, November 19, 2000 6:00 PM</FONT>
<BR><FONT SIZE=3D2>To: Multiple recipients of list ORACLE-L</FONT>
<BR><FONT SIZE=3D2>Subject: Race condition in Oracle C =
Interface?</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>I am trying to use Oracle in a multithreaded = application that creates</FONT>
<BR><FONT SIZE=3D2>and destroys multiple database connections.&nbsp; It = seems to have tickled</FONT>
<BR><FONT SIZE=3D2>a bug in Oracle, though, because the application = hangs up deep in</FONT>
<BR><FONT SIZE=3D2>Oracle.&nbsp; I simplified the application into the = trivial C program</FONT>
<BR><FONT SIZE=3D2>appended below, which on my system locks up about = half of the time.</FONT>
<BR><FONT SIZE=3D2>It just opens up two database connections and some = associated</FONT>
<BR><FONT SIZE=3D2>structures and then closes them.&nbsp; I don't think = there is a problem</FONT>
<BR><FONT SIZE=3D2>with code, but if you are sceptical just take a = look; it is quite</FONT>
<BR><FONT SIZE=3D2>trivial.</FONT>
</P>

<P><FONT SIZE=3D2>Has anybody seen this problem?&nbsp; Does anyone know = a workaround?</FONT>
</P>

<P><FONT SIZE=3D2>I am running Oracle 8.1.6.1 under Linux/Intel with = kernel version</FONT>
<BR><FONT SIZE=3D2>2.2.14.&nbsp; The program locks up within = OCIServerDetach and has to be</FONT>
<BR><FONT SIZE=3D2>kill -6'ed or stronger.&nbsp; Only the client seems = to lock up; the server</FONT>
<BR><FONT SIZE=3D2>seems unaffected.</FONT> </P>

<P><FONT SIZE=3D2>Thanks,</FONT>
<BR><FONT SIZE=3D2>Michael</FONT>
</P>

<P><FONT SIZE=3D2>--</FONT>
<BR><FONT SIZE=3D2>Michael Haggerty</FONT>
<BR><FONT SIZE=3D2>mhagger_at_alum.mit.edu</FONT> </P>
<P><FONT SIZE=3D2>/* =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D main.c =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
 */</FONT>
</P>

<P><FONT SIZE=3D2>/* compile with:</FONT> </P>

<P><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; gcc =
-I/ora8/m01/app/oracle/product/8.1.6/rdbms/demo =
-I/ora8/m01/app/oracle/product/8.1.6/network/public =
-I/ora8/m01/app/oracle/product/8.1.6/plsql/public =
-I/ora8/m01/app/oracle/product/8.1.6/rdbms/public -DORACLE8i -g -o main =
-L/ora8/m01/app/oracle/product/8.1.6/lib/ -lclntsh =
-Wl,-rpath,/ora8/m01/app/oracle/product/8.1.6/lib ./main.c</FONT></P>

<P><FONT SIZE=3D2>*/</FONT>
</P>

<P><FONT SIZE=3D2>#include &lt;assert.h&gt;</FONT>
<BR><FONT SIZE=3D2>#include &lt;stdio.h&gt;</FONT>
<BR><FONT SIZE=3D2>#include &lt;string.h&gt;</FONT>
<BR><FONT SIZE=3D2>#include &lt;malloc.h&gt;</FONT>
<BR><FONT SIZE=3D2>#include &lt;oci.h&gt;</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>typedef struct {</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>OCIEnv&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = *envhp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Environment handle &nbsp; = */</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>OCIError&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *errhp; =

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Error handle =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>OCIServer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *srvhp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Server =
handle&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>OCISession&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *usrhp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* User handle&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>OCISvcCtx&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *svchp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Service =
Context&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */</FONT>
<BR><FONT SIZE=3D2>} ServerContext;</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>ServerContext *Connect(char *user, char *password, = char *database) {</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>ServerContext *sc;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>int&nbsp;&nbsp;&nbsp;&nbsp; userlen;</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>int&nbsp;&nbsp;&nbsp;&nbsp; passwordlen;</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>int&nbsp;&nbsp;&nbsp;&nbsp; databaselen;</FONT>

<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>sword&nbsp;&nbsp; status;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>sword&nbsp;&nbsp; credentials =3D OCI_CRED_RDBMS;</FONT> </P>

<P><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; userlen = =3D strlen(user);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = passwordlen =3D strlen(password);</FONT> <BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = databaselen =3D strlen(database);</FONT> </P>

<P><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sc =3D = (ServerContext *)malloc(sizeof(ServerContext));</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =

SIZE=3D2>sc-&gt;envhp =3D NULL;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>sc-&gt;errhp =3D NULL;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>sc-&gt;svchp =3D NULL;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>sc-&gt;srvhp =3D NULL;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT =
SIZE=3D2>sc-&gt;usrhp =3D NULL;</FONT>

</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>status =3D = OCIEnvCreate(&amp;sc-&gt;envhp, OCI_THREADED, NULL, NULL, NULL, = NULL,</FONT>
<BR><FONT =

SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0, NULL);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* = Allocate the error handle */</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>status = =3D OCIHandleAlloc(sc-&gt;envhp, (dvoid **)&amp;sc-&gt;errhp,</FONT> <BR><FONT =

SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OCI_HTYPE_ERROR, 0, =
NULL);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* = Allocate the server handle */</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>status = =3D OCIHandleAlloc(sc-&gt;envhp, (dvoid **) &amp;sc-&gt;srvhp,</FONT> <BR><FONT =

SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OCI_HTYPE_SERVER, =
0, NULL);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* Connect = to database */</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>status = =3D OCIServerAttach(sc-&gt;srvhp, sc-&gt;errhp, database, = databaselen,</FONT>
<BR><FONT =

SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
OCI_DEFAULT);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* = Allocate a service context */</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>status = =3D OCIHandleAlloc(sc-&gt;envhp, (dvoid **) &amp;sc-&gt;svchp,</FONT> <BR><FONT =

SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OCI_HTYPE_SVCCTX, =
0, NULL);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* Attach = the server to the service context */</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>status = =3D OCIAttrSet(sc-&gt;svchp, OCI_HTYPE_SVCCTX, (dvoid *) = sc-&gt;srvhp,</FONT>
<BR><FONT =

SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp; (ub4) 0, OCI_ATTR_SERVER, =
sc-&gt;errhp);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* = Allocate a session */</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>status = =3D OCIHandleAlloc(sc-&gt;envhp, (dvoid **) &amp;sc-&gt;usrhp,</FONT> <BR><FONT =

SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OCI_HTYPE_SESSION, =
0, NULL);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* Were = credentials provided? if so, set them in the session */</FONT> <BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; status = =3D OCIAttrSet(sc-&gt;usrhp, OCI_HTYPE_SESSION,</FONT> <BR><FONT =

SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp; (dvoid *) user, (ub4) userlen,</FONT>
<BR><FONT =
SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp; OCI_ATTR_USERNAME, sc-&gt;errhp);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; status =3D = OCIAttrSet(sc-&gt;usrhp, OCI_HTYPE_SESSION,</FONT> <BR><FONT =

SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp; (dvoid *) password, (ub4) =
passwordlen,</FONT>
<BR><FONT =
SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp; OCI_ATTR_PASSWORD, sc-&gt;errhp);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
assert(status =3D=3D OCI_SUCCESS);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* Now =
begin the session */</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>status = =3D OCISessionBegin(sc-&gt;svchp, sc-&gt;errhp, sc-&gt;usrhp,</FONT> <BR><FONT =
SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; credentials, =
OCI_DEFAULT);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* Now = attach the session to the service context */</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>status = =3D OCIAttrSet(sc-&gt;svchp, OCI_HTYPE_SVCCTX,</FONT> <BR><FONT =

SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp; sc-&gt;usrhp, 0, OCI_ATTR_SESSION, =
sc-&gt;errhp);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>return = sc;</FONT>
<BR><FONT SIZE=3D2>}</FONT>
</P>

<P><FONT SIZE=3D2>/*</FONT>
<BR><FONT SIZE=3D2>** ServerContext_dealloc</FONT>
<BR><FONT SIZE=3D2>**</FONT>
<BR><FONT SIZE=3D2>** Called when a server context goes out of scope; =
hang up the connection</FONT>
<BR><FONT SIZE=3D2>** to the database!</FONT>
<BR><FONT SIZE=3D2>*/</FONT>
</P>

<P><FONT SIZE=3D2>void ServerContext_dealloc(ServerContext *self) = {</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>sword = status;</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* End the = session */</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>assert(self-&gt;svchp);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; status = =3D OCISessionEnd(self-&gt;svchp, self-&gt;errhp, = self-&gt;usrhp,</FONT>
<BR><FONT =

SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OCI_DEFAULT);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* = Disconnect from the server */</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>assert(self-&gt;srvhp);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; status = =3D OCIServerDetach(self-&gt;srvhp, self-&gt;errhp, = OCI_DEFAULT);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* = Deallocate the session handle */</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = assert(self-&gt;usrhp);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; status = =3D OCIHandleFree(self-&gt;usrhp, OCI_HTYPE_SESSION);</FONT> <BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* = Deallocate the server handle */</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = assert(self-&gt;srvhp);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; status = =3D OCIHandleFree(self-&gt;srvhp, OCI_HTYPE_SERVER);</FONT> <BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* = Deallocate the service context handle */</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT = SIZE=3D2>assert(self-&gt;svchp);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; status = =3D OCIHandleFree(self-&gt;svchp, OCI_HTYPE_SVCCTX);</FONT> <BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* = Deallocate the error handle */</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = assert(self-&gt;errhp);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; status = =3D OCIHandleFree(self-&gt;errhp, OCI_HTYPE_ERROR);</FONT> <BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>/* = Deallocate the environment handle */</FONT> <BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = assert(self-&gt;envhp);</FONT>

<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; status =
=3D OCIHandleFree(self-&gt;envhp, OCI_HTYPE_ENV);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
assert(status =3D=3D OCI_SUCCESS);</FONT> </P>

<P><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = free(self);</FONT>
<BR><FONT SIZE=3D2>}</FONT>
</P>

<P><FONT SIZE=3D2>int main(int argc, char *argv[]) {</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; char *user;</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; char *password;</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; char *database;</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; ServerContext *db1;</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; ServerContext *db2;</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; assert(argc =3D=3D 4);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; user =3D argv[1];</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; password =3D argv[2];</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; database =3D argv[3];</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; db1 =3D Connect(user, password, =
database);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; db2 =3D Connect(user, password, = database);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; fprintf(stderr, &quot;Deleting = db2.\n&quot;);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; =
ServerContext_dealloc(db2);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; fprintf(stderr, &quot;Deleting = db1.\n&quot;);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; =
ServerContext_dealloc(db1);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; fprintf(stderr, &quot;This line = often not reached.\n&quot;);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; return 0;</FONT>
<BR><FONT SIZE=3D2>}</FONT>
</P>

<P><FONT SIZE=3D2>-- </FONT>
<BR><FONT SIZE=3D2>Please see the official ORACLE-L FAQ: <A = HREF=3D"http://www.orafaq.com" =
TARGET=3D"_blank">http://www.orafaq.com</A></FONT>

<BR><FONT SIZE=3D2>-- </FONT>
<BR><FONT SIZE=3D2>Author: Michael Haggerty</FONT>
<BR><FONT SIZE=3D2>&nbsp; INET: mhagger_at_alum.mit.edu</FONT>
</P>

<P><FONT SIZE=3D2>Fat City Network Services&nbsp;&nbsp;&nbsp; -- (858) = 538-5051&nbsp; FAX: (858) 538-5051</FONT> <BR><FONT SIZE=3D2>San Diego, =
California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- Public Internet = Received on Mon Nov 20 2000 - 09:31:00 CST

Original text of this message

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