4848
4949#include < iostream>
5050
51+ // Error numbers to set the drop_sess flag in sessionRelease()
52+ #define DPI_CONNERR_INVALID_SESS 22
53+ #define DPI_CONNERR_SESS_KILLED 28
54+ #define DPI_CONNERR_SESS_MARKED_KILL 31
55+ #define DPI_CONNERR_SESS_TERM_NO_REPLY 45
56+ #define DPI_CONNERR_ORA_NOT_LOGGED_ON 1012
57+ #define DPI_CONNERR_MAX_IDLE_TIMEOUT 2396
58+
5159using namespace std ;
5260
5361
@@ -80,7 +88,7 @@ ConnImpl::ConnImpl(EnvImpl *env, OCIEnv *envh, bool externalAuth,
8088
8189try : env_(env), pool_(NULL ),
8290 envh_(envh), errh_(NULL ), auth_(NULL ), svch_(NULL ), sessh_(NULL ),
83- hasTxn_(false )
91+ hasTxn_(false ), srvh_( NULL ), dropConn_( false )
8492{
8593
8694 this ->initConnImpl ( false , externalAuth, connClass,
@@ -122,7 +130,7 @@ ConnImpl::ConnImpl(PoolImpl *pool, OCIEnv *envh, bool externalAuth,
122130
123131try : env_(NULL ), pool_(pool),
124132 envh_(envh), errh_(NULL ), auth_(NULL ),
125- svch_(NULL ), sessh_(NULL ), hasTxn_(false )
133+ svch_(NULL ), sessh_(NULL ), hasTxn_(false ), srvh_( NULL ), dropConn_( false )
126134{
127135 this ->initConnImpl ( true , externalAuth, connClass, poolName, poolNameLen,
128136 " " , " " );
@@ -473,6 +481,46 @@ void ConnImpl::breakExecution()
473481 }
474482}
475483
484+ /* ****************************************************************************/
485+ /*
486+ DESCRIPTION
487+ set the flag if the non-recoverable error happens to connection
488+
489+ PARAMETERS
490+ errNum - Error number
491+
492+ RETURNS:
493+ -NONE_
494+ */
495+ void ConnImpl::setErrState ( int errNum )
496+ {
497+ /*
498+ * This flag applicable for only Pool, non pooled connection anyway gets
499+ * terminated upon release. This code is NOT thread-safe. But, we are only
500+ * setting the flag to TRUE - multiple threads can try and only will update
501+ * to TRUE only, so it is okay setting this flag.
502+ */
503+
504+ if ( pool_ )
505+ {
506+ switch ( errNum )
507+ {
508+ // Error numbers to set drop_sess flag
509+ case DPI_CONNERR_INVALID_SESS:
510+ case DPI_CONNERR_SESS_KILLED:
511+ case DPI_CONNERR_SESS_MARKED_KILL:
512+ case DPI_CONNERR_SESS_TERM_NO_REPLY:
513+ case DPI_CONNERR_ORA_NOT_LOGGED_ON:
514+ case DPI_CONNERR_MAX_IDLE_TIMEOUT:
515+ dropConn_ = true ;
516+ break ;
517+
518+ default :
519+ break ;
520+ }
521+ }
522+ }
523+
476524
477525/* ---------------------------------------------------------------------------
478526 PRIVATE METHODS
@@ -499,7 +547,6 @@ void ConnImpl::initConnImpl ( bool pool, bool externalAuth,
499547 const string& connClass, OraText *poolNmRconnStr,
500548 ub4 nameLen, const string &user, const string &password )
501549{
502- OCIServer *srvh = NULL ;
503550 ub4 mode = OCI_DEFAULT;
504551 ub2 csid = 0 ;
505552
@@ -551,14 +598,15 @@ void ConnImpl::initConnImpl ( bool pool, bool externalAuth,
551598 OCI_ATTR_SESSION, errh_ ), errh_ );
552599
553600 // Initialize the server handle from service handle
554- ociCall ( OCIAttrGet ( svch_, OCI_HTYPE_SVCCTX, ( void * ) &srvh , 0 ,
601+ ociCall ( OCIAttrGet ( svch_, OCI_HTYPE_SVCCTX, ( void * ) &srvh_ , 0 ,
555602 ( ub4 ) OCI_ATTR_SERVER, errh_ ), errh_ );
556603
557604 // Get the DBCHARSET from server
558- ociCall ( OCIAttrGet ( srvh , ( ub4 ) OCI_HTYPE_SERVER, ( void * ) &csid,
605+ ociCall ( OCIAttrGet ( srvh_ , ( ub4 ) OCI_HTYPE_SERVER, ( void * ) &csid,
559606 ( ub4 * ) 0 , ( ub4 ) OCI_ATTR_CHARSET_ID, errh_ ),
560607 errh_ );
561- csratio_ = getCsRatio ( csid );
608+
609+ csratio_ = getCsRatio ( csid );
562610}
563611
564612/* ****************************************************************************/
@@ -579,9 +627,25 @@ void ConnImpl::initConnImpl ( bool pool, bool externalAuth,
579627
580628void ConnImpl::cleanup ()
581629{
630+ ub4 relMode = OCI_DEFAULT;
631+ ub4 serverStatus = OCI_SERVER_NORMAL;
632+
582633 if (svch_)
583634 {
584- OCISessionRelease (svch_, errh_, NULL , 0 , OCI_DEFAULT);
635+ if ( pool_ )
636+ {
637+ // Get the connection status
638+ if ( !dropConn_ )
639+ ociCall ( OCIAttrGet ( ( void * ) srvh_, OCI_HTYPE_SERVER,
640+ ( void * ) &serverStatus, ( ub4 * ) 0 ,
641+ OCI_ATTR_SERVER_STATUS, errh_ ), errh_ );
642+
643+ // Remove the session from pool in case of unusable
644+ if ( dropConn_ || ( serverStatus != OCI_SERVER_NORMAL ) )
645+ relMode |= OCI_SESSRLS_DROPSESS;
646+ }
647+
648+ OCISessionRelease (svch_, errh_, NULL , 0 , relMode);
585649 svch_ = NULL ;
586650 }
587651
0 commit comments