Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

OCI concurrent access example

170 views
Skip to first unread message

Simeon Bailey

unread,
Sep 1, 1999, 3:00:00 AM9/1/99
to
Hey, :-)
Would somebody be able to supply me with an example of concurrent
access using OCI. The problem I am experiencing is random hanging of the
OCIServerAttach call.

Any thoughts or suggestions would be greatly appreciated.

Many thanks
S.


Markus M. Mueller

unread,
Sep 1, 1999, 3:00:00 AM9/1/99
to
Have you tried, to give each thread a dedicated connection?
In my application each thread has even it's own OCIEnv pointer.

give it a try.
Markus

Simeon Bailey

unread,
Sep 2, 1999, 3:00:00 AM9/2/99
to
"Markus M. Mueller" wrote:

> Have you tried, to give each thread a dedicated connection?
> In my application each thread has even it's own OCIEnv pointer.
>
> give it a try.

No Luck, I simplified it to just connecting an disconnecting in a loop and it
still hangs
This is simple stuff.
It is almost exactly oracle example code!


OCIInitialize((ub4) OCI_THREADED, NULL, NULL, NULL, NULL);
while (1) {

OCIEnvInit(&envhp, OCI_DEFAULT, 0, (dvoid **)0);

status = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR,
0, (dvoid **) 0);
if (status != OCI_SUCCESS)
cerr << "Alloc Error Hndl failed" << endl;

status = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &srvhp,
OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0);
check_status(status);

status = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &svchp,
OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0);
check_status(status);

// This is the call that hangs!! periodically
status = OCIServerAttach(srvhp, errhp, (text *)0, 0, OCI_DEFAULT);
check_status(status);

status = OCIAttrSet((dvoid *) svchp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp,
(ub4) 0, OCI_ATTR_SERVER, (OCIError *) errhp);
check_status(status);

status = OCIHandleAlloc((dvoid *) envhp, (dvoid **)&authp, (ub4)
OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0);
check_status(status);

status = OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,
(dvoid *) "snoopdogg", (ub4) strlen((char *)"snoopdogg"),
(ub4) OCI_ATTR_USERNAME, errhp);
check_status(status);

status = OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,
(dvoid *) "thepass", (ub4) strlen((char *)"thepass"),

status = OCISessionBegin(svchp, errhp, authp, OCI_CRED_RDBMS,
(ub4) OCI_DEFAULT);
check_status(status);

status = OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,
(dvoid *) authp, (ub4) 0,
(ub4) OCI_ATTR_SESSION, errhp);
check_status(status);

/// Clean up and disconnect

status = OCISessionEnd(svchp, errhp, authp,
(ub4) OCI_DEFAULT);
check_status(status);

status = OCIServerDetach(srvhp, errhp, OCI_DEFAULT);
check_status(status);

status = OCIHandleFree(authp, OCI_HTYPE_SESSION);
check_status(status);

status = OCIHandleFree(srvhp, OCI_HTYPE_SERVER);
check_status(status);

status = OCIHandleFree(svchp, OCI_HTYPE_SVCCTX);
check_status(status);

status = OCIHandleFree(errhp, OCI_HTYPE_ERROR);
check_status(status);

if (envhp) {
status = OCIHandleFree(envhp, OCI_HTYPE_ENV);
if (status != OCI_SUCCESS)
cerr << "Release Oracle environment handle failed";
}
}


Markus M. Mueller

unread,
Sep 2, 1999, 3:00:00 AM9/2/99
to
the following sample works on my system
Solaris 2.7, Oracle 8.0.5.1

markus

/**********************************************************

**********************************************************/
#include <iostream>
#include <oci.h>

using namespace std;

#define ORA_USER "system"
#define ORA_PW "yourpw"

#define ORA_CONNECT "dbora1" // connect string from TNS ora file

void ORA_DispError( char *pszFile,
char *pszFunc,
int nLineNo,
OCIError *errhp,
sword status )
{
text errbuf[512];
ub4 buflen;
sb4 errcode;
char *pszOraStatus = NULL;
char *ora_status_tbl[] = { "OCI_SUCCESS" , /* index 0 */
"OCI_SUCCESS_WITH_INFO", /* 1 */
"OCI_NEED_DATA", /* 2 */
"OCI_NO_DATA", /* 3 */
"OCI_ERROR", /* 4 */
"OCI_INVALID_HANDLE", /* 5 */
"OCI_STILL_EXECUTING", /* 6 */
"OCI_CONTINUE", /* 7 */
"OCI_unknown" /* 8 */
};
switch (status)
{
case OCI_SUCCESS:
pszOraStatus = ora_status_tbl[0];
break;

case OCI_SUCCESS_WITH_INFO:
pszOraStatus = ora_status_tbl[1];
break;

case OCI_NEED_DATA:
pszOraStatus = ora_status_tbl[2];
break;

case OCI_NO_DATA:
pszOraStatus = ora_status_tbl[3];
break;

case OCI_ERROR:
pszOraStatus = ora_status_tbl[4];
break;

case OCI_INVALID_HANDLE:
pszOraStatus = ora_status_tbl[5];
break;

case OCI_STILL_EXECUTING:
pszOraStatus = ora_status_tbl[6];
break;

case OCI_CONTINUE:
pszOraStatus = ora_status_tbl[7];
break;

default:
pszOraStatus = ora_status_tbl[8];
break;
}

if ( status != OCI_ERROR && status != OCI_NO_DATA )
{
printf( "status: %s\n", pszOraStatus );
printf( "file : %s line : %d\n", pszFile, nLineNo );
}
else
{
OCIErrorGet ( (dvoid *) errhp, (ub4) 1, (text *) NULL,
(sb4*)&errcode,
errbuf, (ub4) sizeof(errbuf), (ub4)
OCI_HTYPE_ERROR );
printf( "file : %s line : %d\n", pszFile, nLineNo );
printf( "error : %s\n text: %s\n", pszOraStatus, errbuf );
}
}

void check_status( sword ora_status )
{
switch( ora_status )
{
case OCI_SUCCESS:
case OCI_SUCCESS_WITH_INFO:

cout << "OCI: success" << endl;
break;

default:

cout << "OCI: ERROR" << endl;
}
return;
}

int main( void )
{
int ii = 0;

OCIEnv *envhp;
OCIServer *srvhp;
OCIError *errhp;
OCISvcCtx *svchp;
OCISession*authp;
sword status;

cout << "start..." << endl;

OCIInitialize((ub4) OCI_THREADED, NULL, NULL, NULL, NULL);

for( ii=0; ii < 10; ii++ )


{
OCIEnvInit(&envhp, OCI_DEFAULT, 0, (dvoid **)0);

status = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp,

OCI_HTYPE_ERROR,0, (dvoid **) 0);


if (status != OCI_SUCCESS)
cerr << "Alloc Error Hndl failed" << endl;

status = OCIHandleAlloc((dvoid *) envhp, (dvoid **)

&srvhp,OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0);
check_status(status);

status = OCIHandleAlloc((dvoid *) envhp, (dvoid **)

&svchp,OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0);
check_status(status);
cout << "OCIServerAttach...";

// This is the call that hangs!! periodically

//
// ON MY SYSTEM IT'S NOT HANGING (markus mueller)
//
status = OCIServerAttach(srvhp, errhp,
(text*)ORA_CONNECT,strlen(ORA_CONNECT), OCI_DEFAULT);
cout << "returned" << endl;
check_status(status);


status = OCIAttrSet((dvoid *) svchp, OCI_HTYPE_SVCCTX, (dvoid

*)srvhp,(ub4) 0, OCI_ATTR_SERVER, (OCIError *) errhp);
check_status(status);

status = OCIHandleAlloc((dvoid *) envhp, (dvoid **)&authp,

(ub4)OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0);
check_status(status);

status = OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,

(dvoid *) ORA_USER, (ub4) strlen((char *)ORA_USER),
(ub4) OCI_ATTR_USERNAME, errhp);
check_status(status);

status = OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,

(dvoid *) ORA_PW, (ub4) strlen((char *)ORA_PW),
(ub4) OCI_ATTR_PASSWORD, errhp);

status = OCISessionBegin(svchp, errhp, authp, OCI_CRED_RDBMS,
(ub4) OCI_DEFAULT);

cout << "OCISessionBegin" << endl;
check_status(status);
ORA_DispError( __FILE__, "main", __LINE__,errhp,status );

status = OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,
(dvoid *) authp, (ub4) 0,
(ub4) OCI_ATTR_SESSION, errhp);
check_status(status);

/// Clean up and disconnect

status = OCISessionEnd(svchp, errhp, authp,
(ub4) OCI_DEFAULT);
check_status(status);

status = OCIServerDetach(srvhp, errhp, OCI_DEFAULT);
check_status(status);

status = OCIHandleFree(authp, OCI_HTYPE_SESSION);
check_status(status);

status = OCIHandleFree(srvhp, OCI_HTYPE_SERVER);
check_status(status);

status = OCIHandleFree(svchp, OCI_HTYPE_SVCCTX);
check_status(status);

status = OCIHandleFree(errhp, OCI_HTYPE_ERROR);
check_status(status);

if (envhp) {
status = OCIHandleFree(envhp, OCI_HTYPE_ENV);
if (status != OCI_SUCCESS)
cerr << "Release Oracle environment handle failed";
}
}

cout << "### end ###" << endl;

Simeon Bailey

unread,
Sep 3, 1999, 3:00:00 AM9/3/99
to
Hey :-),
If you increase the num of iterations from 10 to 100 in your prog, you should
see the hang (I do). You may have to run it a couple of times though.

System - Solaris 2.6 Oracle 8.0.5
Many thanks for the time you have spent on this, greatly appreciated.

S.

0 new messages