首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

occi 连接池 联接失效

2013-01-23 
occi 连接池 连接失效服务器长时间运行,难免会连接池失效。当连接实效会导致进程卡死,好多才能入库城成功,

occi 连接池 连接失效
服务器长时间运行,难免会连接池失效。当连接实效会导致进程卡死,好多才能入库城成功,或是抛出异常。考虑过定期检测连接有效性,无效则重新创建。但occi 貌似没有提供类似检测的接口。 只有和oracle 交互才能检测。 但交互势必又要在连接池中获取连接,依旧可能获取无效连接。 求教这该如何是好。

这是class

class OraConnPool
{
protected:
OraConnPool();

public:
bool createConnPool(const std::string& username, const std::string& passwd, const std::string& connectString, int maxCon, int minCon, int incrCon);
bool reCreateConnPool();
void destroyConnPool();
string getErrData(void);

Connection * getConnection();
void freeConnection(Connection*);

static OraConnPool* getInstance();
bool checkConnect();
~OraConnPool();
private:
Environment * env;
StatelessConnectionPool * scp;
string sErrInfo;
static OraConnPool* _instance;
std::string username;
std::string password;
std::string connstring;
 int maxCon;
 int minCon;
 int incrCon;
};


这个创建连接池
bool OraConnPool::createConnPool(const std::string& username, const std::string& passwd, const std::string& connectString, int maxCon, int minCon, int incrCon)
{
this->username = username;
this->password = passwd;
this->connstring = connectString;
this->maxCon = maxCon;
this->minCon = minCon;
this->incrCon = incrCon;
setenv("NLS_LANG", "SIMPLIFIED CHINESE_CHINA.ZHS32GB18030", 1);
//setenv("NLS_LANG", "american_america.AL32UTF8", 1);
sErrInfo="";

try
{
//env = Environment::createEnvironment(Environment::DEFAULT);
env = Environment::createEnvironment(Environment::Mode(Environment::OBJECT|Environment::THREADED_MUTEXED));
if (env == NULL)
{
sErrInfo="CreateConnpool Error!";
return false;
}


scp = env->createStatelessConnectionPool(username.c_str(), passwd.c_str(), connectString.c_str(), maxCon, minCon, incrCon, StatelessConnectionPool::HOMOGENEOUS);
scp ->setTimeOut(10000);
if (scp == NULL)
{
Environment::terminateEnvironment(env);
sErrInfo="Connection oracle error!";
return false;
}

sErrInfo="CreateConnpool success! ";
return true;
}
catch(SQLException ex)
{
sErrInfo=ex.getMessage().c_str();
}
return false;
}


这是获取链接
Connection* OraConnPool::getConnection()
{
Connection * pConn = NULL;
pConn = scp->getConnection();

return pConn;
}

[解决办法]
仅供参考:
int DBOperator::connectionKeepAlive() {
    //DBManager<DBOracle> m_db_manager(g_db, g_user, g_passwd, g_host, 1, g_log_level);
    //Connection_Guard<DBOracle> connGuard(&m_db_manager);
    Connection_Guard<DBOracle> connGuard( _p_dbm.get() );
    oracle::Connection* p_conn = connGuard.getConnection();
    if ( !p_conn ) {


        return -1;
    }
    try
    {
        ACE_Auto_Ptr< oracle::Statement > statementPtr( p_conn->createStatement( "SELECT sysdate from DUAL" ) );
//      ACE_DEBUG( ( LM_INFO, "[%D %T] [INFO] (%N:%l)\tDBOperator::connectionKeepAlive() SQL: SELECT sysdate from DUAL\n") );
        ACE_Auto_Ptr< oracle::Resultset > resultSet( statementPtr->executeQuery() );
        if (!resultSet->next() ) {
            return -1;
        }
    }catch ( oracle::SQLException& ex )
    {
        ACE_DEBUG( ( LM_ERROR, "[%D %T] [ERROR] (%N:%l)\tDBOperator::connectionKeepAlive() SQL Exception : %s\n", ex.getMsg().c_str() ) );
        return -3;
    }
    return 0;
}
int BizCtrlManager::svc()
{
    int cnt;
    ACE_DEBUG( ( LM_INFO, "[%D %T] [INFO] (%N:%l)\tKeepALiveThread::svc() Thread Begin !!!  \n") );
    ACE_Message_Block *mb;
    ACE_Time_Value now ;
    ACE_Time_Value interval(IMSERVERCONFIGAPP->getBizCtrlKeepLiveTime() );
    setLoopFlag( true );
    cnt=0;
    while (1) {
        ACE_DEBUG( ( LM_ERROR, "[%D %T] [INFO] (%N:%l)\t ***** [KeepALiveThread::svc()]  Client Number IS %d !!!  \n",IMCLIENTHDLERMAPMANAGERAPP->getClientNumber() ) );
        cnt++;
        if (cnt>=10) {
            cnt=0;
            int res = IMSERVERDBAPP->connectionKeepAlive();
            ACE_DEBUG( ( LM_INFO, "[%D %T] [INFO] (%N:%l)\tBizCtrlManager::svc() connectionKeepAlive() Return %d \n",res  ) );
        }
        now = ACE_OS::gettimeofday();
        if ( getq( mb, &( now + interval) ) != -1 ) {
            Message_Block_Guard message_block_guard (mb);
            char *c_data = mb->base();
            if ( c_data ) {
                void *v_data = (void *)c_data;
                unsigned int *message = ( unsigned int * )v_data;


                if ( BIZCTRL_EXIT == *message ) {
                    delete message;
                    ACE_DEBUG( ( LM_INFO, "[%D %T] [INFO] (%N:%l)\tKeepALiveThread::svc() Get Exit Message !!!  \n") );
                    break;//
                } else {
                    if ( BIZCTRL_RECONNECT == *message ) {
                        delete message;
                        ACE_DEBUG( ( LM_ERROR, "[%D %T] [ERROR] (%N:%l)\tKeepALiveThread::svc() Get ReConnect BizCtrl Message Close All Client Handler !!!  \n" ) );
                        IMCLIENTHDLERMAPMANAGERAPP->closeAllClientHandle();
                        while ( !imSRegister() && getLoopFlag() ) {
                            ACE_DEBUG( ( LM_ERROR, "[%D %T] [ERROR] (%N:%l)\tKeepALiveThread::svc() ReConnect BizCtrl Error Retry !!!  \n" ) );
                            ACE_OS::sleep( ACE_Time_Value( 10 ) );
                        }
                        //setRegisterFlag( true );
                        continue;
                    }
                }
            }
        }
        if ( !imSKeepAlive() ) {
            IMCLIENTHDLERMAPMANAGERAPP->closeAllClientHandle();
        }
    }
    _thrIsALive = false;
    ACE_DEBUG( ( LM_INFO, "[%D %T] [INFO] (%N:%l)\tKeepALiveThread::svc() Keep ALive Thread Exit!!!\n" ) );
    return 0;


}

热点排行