Environment * m_env=Environment::createEnvironment(Environment::DEFAULT);
ConnectionPool *m_connPool = m_env->createConnectionPool("aaa","bbb","",3,4,1);
//至此运行成功,监控数据库连接多了3个。//下面三句,数据库连接又多了3个,连接数已达到6个。为何未用连接池中3个中的一个???
Connection *conn1 = m_connPool->createConnection("aaa","bbb");
Connection *conn2 = m_connPool->createConnection("aaa","bbb");
Connection *conn3 = m_connPool->createConnection("aaa","bbb");//
m_connPool->terminateConnection(conn1);
m_connPool->terminateConnection(conn2);
m_connPool->terminateConnection(conn3);
//新建3个连接断开成功。m_env->terminateConnectionPool(m_connPool);
Environment::terminateEnvironment(m_env);
//断开连接池与环境皆成功。环境:linux as4。自带gcc,版本3.4.6。oracle10g,libocci10a、libocci.so.10.1已升级到2005.12.6发布的,原安装盘安装的日期为2005.6.28。

解决方案 »

  1.   

    你调用的是ConnectionPool::createConnection,当然会再创建三个连接,没问题啊, 你不建就是了。
      

  2.   

    谢谢mosaic答复,那如何取得连接池中任一连接?
      

  3.   

    可以试试最小连接数设为0。如果是10g以上版本,可使用StatelessConnectionPool,其中包含getConnection方法。
      

  4.   

    你这个问题很简单, 我曾经遇到过, 原因是你创建连接池的是createConnectionPool(),它的意思是创建永久连接,当你要获取一个连接时并不是从连接池里面获取的。 给你一段代码程序:#include "OcciConnectPool.h"
    #include "App.h"extern CApp theApp;
    COcciConnectPool::COcciConnectPool(const string& strUserName, 
    const string& strPassword, 
    const string& strDBName,
    unsigned int iMinConn, 
    unsigned int iMaxConn, 
    unsigned int iIncrConn)
    {
    m_pEnv = NULL;
    m_pConnPool = NULL; m_strUserName = strUserName;
    m_strPassword = strPassword;
    m_strDBName = strDBName;
    m_iMinConn = iMinConn;
    m_iMaxConn = iMaxConn;
    m_iIncrConn = iIncrConn;
    }

    bool COcciConnectPool::Init(void)
    {
    try 

    m_pEnv = Environment::createEnvironment();

    if (!m_pEnv)
    {
    return false;
    }

    m_pConnPool = m_pEnv->createStatelessConnectionPool(m_strUserName, 
    m_strPassword, 
    m_strDBName, 
    m_iMaxConn, 
    m_iMinConn, 
    m_iIncrConn, 
    StatelessConnectionPool::HOMOGENEOUS);

    if (!m_pConnPool)
    {
    return false;
    }

    m_pConnPool->setTimeOut(900);

    StatelessConnectionPool::BusyOption BusyOption = StatelessConnectionPool::FORCEGET;
    m_pConnPool->setBusyOption(BusyOption);

    catch (SQLException &ex) 
    {
    string log = "ConnectionPool Init Error: " + ex.getMessage();
    theApp.WriteLog(LOG_LEVEL_ERROR, log);
    return false;
    }
    catch (exception &excp)
    {
    string log = "ConnectionPool Init Error: ";
    log.append(excp.what());
    theApp.WriteLog(LOG_LEVEL_ERROR, log);
    return false;
    }

    return true;
    }
    COcciConnectPool::~COcciConnectPool(void)
    {
    try
    {
    if (m_pConnPool != NULL)
    {
    m_pEnv->terminateStatelessConnectionPool(m_pConnPool);
    m_pConnPool = NULL;
    } if (m_pEnv != NULL)
    {
    Environment::terminateEnvironment(m_pEnv);
    m_pEnv = NULL;
    }
    }
    catch (SQLException &ex) 
    {
    string log = "in ~COcciConnectPool() Error: " + ex.getMessage();
    theApp.WriteLog(LOG_LEVEL_ERROR, log);
    }
    catch (exception &excp)
    {
    string log = "in ~COcciConnectPool() Error:";
    log.append(excp.what());
    theApp.WriteLog(LOG_LEVEL_ERROR, log);
    }
    }/************************************************************************* 从连接池中获取一个连接*************************************************************************/
    Connection* COcciConnectPool::GetConnection()
    {
    Connection* pCon = NULL;

    try
    {
    if (m_pConnPool)
    {
    pCon = m_pConnPool->getConnection();
    }
    }
    catch (SQLException &ex) 
    {
    string log = "get connection from pool Error: " + ex.getMessage();
    theApp.WriteLog(LOG_LEVEL_ERROR, log);
    }
    catch (exception &excp)
    {
    string log = "get connection from pool Error: ";
    log.append(excp.what());
    theApp.WriteLog(LOG_LEVEL_ERROR, log);
    }
    return pCon;
    }/************************************************************************ 释放一个连接************************************************************************/
    bool COcciConnectPool::ReleaseConnection(Connection* pConnect)
    {
    bool ret = false;

    try
    {
    if (m_pConnPool && pConnect)
    {
    m_pConnPool->releaseConnection(pConnect);
    ret = true;
    }
    }
    catch (SQLException &ex) 
    {
    string log = "Release Connect Error: " + ex.getMessage();
    theApp.WriteLog(LOG_LEVEL_ERROR, log);
    }
    catch (exception &excp)
    {
    string log = "Release Connect Error: " ;
    log.append(excp.what());
    theApp.WriteLog(LOG_LEVEL_ERROR, log);
    }

    return ret;
    }