Oracle Database 10g XML SQL [Electronic resources] : Design, Build Manage XML Applications in Java, C, C++ PL/SQL

Mark V. Scardina, Ben Chang, Jinyu Wang

نسخه متنی -صفحه : 218/ 153
نمايش فراداده

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); 
}  
}