Disconnecting from the Database and Cleaning Up
Once your database session is completed, you need to detach from the database and clean up the context by freeing all of the allocated handles. You do this by calling detach_server() in the following generic code, which is not application-dependent./*--------------------------------------------------------*/
/* End the session, detach server, and free handles. */
/*--------------------------------------------------------*/
void ociapi::disconnect_server( boolean tab_exists)
{ sword status = 0;
printf("\n\nLogged off and detached from server.\n");
/* End a session */
if (status = OCISessionEnd((OCISvcCtx *)svchp, (OCIError *)errhp,
(OCISession *)authp, (ub4) OCI_DEFAULT)) {
checkerr( errhp, status);
cleanup();
return;
}
/* Detach from the server */
if (status = OCIServerDetach((OCIServer *)srvhp, (OCIError *)errhp,
(ub4)OCI_DEFAULT)) {
checkerr( errhp, status);
cleanup();
return;
}
/* Free the handles */
if (stmthp) {
OCIHandleFree((dvoid *)stmthp, (ub4) OCI_HTYPE_STMT);
}
if (authp) {
OCIHandleFree((dvoid *)authp, (ub4) OCI_HTYPE_SESSION);
}
if (svchp) {
OCIHandleFree((dvoid *)svchp, (ub4) OCI_HTYPE_SVCCTX);
}
if (srvhp) {
OCIHandleFree((dvoid *)srvhp, (ub4) OCI_HTYPE_SERVER);
}
if (errhp) {
OCIHandleFree((dvoid *)errhp, (ub4) OCI_HTYPE_ERROR);
}
if (envhp) {
OCIHandleFree((dvoid *)envhp, (ub4) OCI_HTYPE_ENV);
}
}
To perform the final bit of housekeeping, you call the cleanup() function to free the OCI environment as a whole:
/*--------------------------------------------------------*/
/* Clean up OCI handles. */
/*--------------------------------------------------------*/
void ociapi::cleanup()
{ if (envhp) {
OCIHandleFree((dvoid *)envhp, OCI_HTYPE_ENV);
}
}