The first step when creating a C++ database application is to perform all of the necessary OCI initializations and allocations. These steps are required for all OCI applications, as the following code illustrates. This init_env_handle() function creates and initializes the OCI environment that requires an allocation of several handles that will be populated by the subsequent member functions. This function is not application-specific and thus can be used as-is in your applications.
/*--------------------------------------------------------*/
/* Initialize the environment and allocate handles */ /*--------------------------------------------------------*/ sb4 ociapi::init_env_handle() { sword status = 0; /* Environment initialization and creation */ if (OCIEnvCreate((OCIEnv **) &envhp, (ub4) OCI_OBJECT, (dvoid *) 0, (dvoid * (*)(dvoid *,size_t)) 0, (dvoid * (*)(dvoid *, dvoid *, size_t)) 0, (void (*)(dvoid *, dvoid *)) 0, (size_t) 0, (dvoid **) 0)) { printf("FAILED: OCIEnvCreate()\n"); return OCI_ERROR; } /* allocate error handle */ if (OCIHandleAlloc( (dvoid *)envhp, (dvoid **) errhp, (ub4) OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0)) { printf("FAILED: OCIHandleAlloc() on errhp\n"); return OCI_ERROR; } /* allocate server handle */ if (status = OCIHandleAlloc( (dvoid*)envhp, (dvoid **) &srvhp, (ub4) OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0)) { printf("FAILED: OCIHandleAlloc() on srvhp\n"); checkerr( errhp, status); return OCI_ERROR; } /* allocate service context handle */ if (status = OCIHandleAlloc(( dvoid*)envhp, (dvoid **) &svchp, (ub4) OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0)) { printf("FAILED: OCIHandleAlloc() on svchp\n"); checkerr( errhp, status); return OCI_ERROR; } /* allocate session handle */ if (status = OCIHandleAlloc( (dvoid*)envhp, (dvoid **) &authp, (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0)) { printf("FAILED: OCIHandleAlloc() on authp\n"); checkerr( errhp, status); return OCI_ERROR; } /* Allocate statement handle */ if (status = OCIHandleAlloc( (dvoid*)envhp, (dvoid **) stmthp, (ub4) OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0)) { printf("FAILED: OCIHandleAlloc() on stmthp\n"); checkerr( errhp, status); return OCI_ERROR; } return OCI_SUCCESS; }