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

Home -> Community -> Usenet -> c.d.o.server -> OCI question -- if you have souls please help!

OCI question -- if you have souls please help!

From: Norman Dunbar <Norman.Dunbar_at_lfs.co.uk>
Date: Tue, 13 May 2003 09:36:01 +0100
Message-ID: <E2F6A70FE45242488C865C3BC1245DA703AAA9E8@lnewton.leeds.lfs.co.uk>


Morning Dave,

what version of OCI are you using - the code you supplied looks very 'old style' to me. There is a chunk of my code below which will connect to a database using Oracle 8i/9i OCI. The code is an extract from a simple OCI application that I am working on using Borland's C++ compiler. The code is in C++ so what you are seeing is the member functions of a Window object that I'm using. Hopefully, you'll be able to extract the meat from mthe following code.

Also, much of the infernal casting that OCI needs has not been shown. Borland C++ compilers are quite able to determine that a cast will work or not, and advises so. I only have to cast when absolutely necessary - I get the impression that the example code & programs for OCI are written to MS Visual C++ standards - which is actually a contradiction as VC++ has no standards !

Cheers,
Norman.

#include "oci.h"
#include <string.h>
#include "mainwindow.h"

//----------------------------------------------------------------------


// Connect to a database using the supplied username, password and service. The
// service may be left blank if we are connecting to a local database and the
// ORACLE_SID environment variable is set correctly.
//----------------------------------------------------------------------


bool MainWindow::ConnectTo(const char *Username, const char *Password, const char *Service)
{

#ifdef ORACLE_8I_ONWARDS

// Initialise the OCI enviroment Oracle 8i/9i style.
    LogMessage("Creating OCI Environment - ", false);     if (OCIEnvCreate(&OCIParams.envhp,

                     OCI_DEFAULT, 
                     NULL, 
                     NULL,
                     NULL,
                     NULL,
                     0,
                     NULL)) {
        LogMessage("failed.");
        return false;
    } else {
        LogMessage("ok.");

    }

#else

// Initialise the OCI enviroment Oracle 8.0 style.
    LogMessage("Initialising OCI - ", false);     if (OCIInitialize(OCI_DEFAULT,

                      NULL, 
                      NULL, 
                      NULL, 
                      NULL)) {
        LogMessage("failed.");
        return false;
    } else {
        LogMessage("ok.");

    }

    LogMessage("Initialising OCI Environment - ", false);     if (OCIEnvInit(&OCIParams.envhp,

                   OCI_DEFAULT, 
                   0, 
                   NULL)) {
        LogMessage("failed.");
        return false;
    } else {
        LogMessage("ok.");

    }
#endif

    LogMessage("Creating OCI error handle - ", false);     if (OCIHandleAlloc(OCIParams.envhp,

                       reinterpret_cast<dvoid **>(&OCIParams.errhp), 
                       OCI_HTYPE_ERROR, 
                       0, 
                       NULL)) {
        LogMessage("failed.");
        return false;
    } else {
        LogMessage("ok.");

    }

    LogMessage("Logging on to database - ", false);     if (OCILogon(OCIParams.envhp,

                 OCIParams.errhp,
                 &OCIParams.svchp,
                 Username,
                 strlen(Username),
                 Password,
                 strlen(Password),
                 Service,
                 strlen(Service))) {
        LogMessage("failed.");
        return false;
    } else {
        LogMessage("ok.");

    }

// We must be connected if we got this far !
    return true;
}

//----------------------------------------------------------------------


// Disconnect from the database and clean up any OCI handles we have allocated.
// Note that I explicitly delete all my handles even though the documentation
// says that Oracle cleans up child handles - sometimes it doesn't seem to do
// it correctly and memory leaks. (At least on Windows).
//----------------------------------------------------------------------


void MainWindow::Disconnect()
{

    LogMessage("Logging off - ", false);     if (OCILogoff(OCIParams.svchp,

                  OCIParams.errhp)) {
       LogMessage("failed.");
    } else {
       LogMessage("ok.");

    }

// Deallocate my handles ...
// There is no need to deallocate the Service Context handle as the
above

// call to OCILogoff deallocates it for us in a manner similar to
that in

// which OCILogon creates it for us.

    LogMessage("Deallocating error handle - ", false);     if (OCIHandleFree(OCIParams.errhp,

                      OCI_HTYPE_ERROR)) {
       LogMessage("failed.");
    } else {
       LogMessage("ok.");

    }

// It is written that freeing the parent handle (the environment)
will also

// free all its children (the error). I have tested this and it is
true, but

// the examples in Oracle explicitly free all user allocated handles
- why ?

    //
// If I change the order of freeing handles and free the environment
one

// first, freeing the error handle afterwards will fail - so it
looks as if

// the documentation is correct. However, I'm not a very trusting
soul, so I

// do it myself.

    LogMessage("Deallocating environment handle - ", false);     if (OCIHandleFree(OCIParams.envhp,

                      OCI_HTYPE_ENV)) {
       LogMessage("failed.");
    } else {
       LogMessage("ok.");

    }
}

Norman Dunbar
Database/Unix administrator
Lynx Financial Systems Ltd.
mailto:Norman.Dunbar_at_LFS.co.uk
Tel: 0113 289 6265
Fax: 0113 289 3146
URL: http://www.Lynx-FS.com
-------------------------------------
Received on Tue May 13 2003 - 03:36:01 CDT

Original text of this message

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