用occi函数封装的连接池类

26 篇文章 0 订阅
 class mConnectionPool
{
        public:
mConnectionPool();
                ~mConnectionPool();
                int connectDB(char *userName,char *userPassWord,char *dbName);
                Connection * getConnection();
                int closeConnection(Connection *con);
        private:
                oracle::occi::Environment *env;
                ConnectionPool *connPool;
                char m_userName[128+1];
                char m_userPassWord[128+1];
                char m_dbName[128+1];
                int maxConn;
                int minConn;
                int incrConn;
};

mConnectionPool::mConnectionPool()
{
        env=NULL;
        connPool=NULL;
        memset(m_userName,0,sizeof(m_userName));
        memset(m_userPassWord,0,sizeof(m_userPassWord));
        memset(m_dbName,0,sizeof(m_dbName));
        maxConn=10;
        minConn=5;
        incrConn=1;
}

mConnectionPool::~mConnectionPool()
{
        try{
                        env->terminateConnectionPool(connPool);
                        Environment::terminateEnvironment(env);
                }
                catch(SQLException &ex)
                {
                        cout<<"release connection error:"<<ex.what()<<endl;
                }
}

int mConnectionPool::connectDB(char *userName,char *userPassWord,char *dbName)
{
        strncpy(m_userName,userName,sizeof(m_userName)-1);
        strncpy(m_userPassWord,userPassWord,sizeof(m_userPassWord)-1);
        strncpy(m_dbName,dbName,sizeof(m_dbName)-1);
        
        try{
                        env = oracle::occi::Environment::createEnvironment(Environment::DEFAULT);
                        connPool = env->createConnectionPool(m_userName,m_userPassWord,m_dbName,minConn,maxConn,incrConn);                      
                }
                catch(SQLException &ex)
                {
                        cout<<"create connPool error:"<<ex.what()<<endl;
                        return 1;
                }
                
                return 0;
}

int mConnectionPool::closeConnection(Connection *con)
{
        try{
                        connPool->terminateConnection(con);
                }
                catch(SQLException &ex)
                {
                        cout<<"release connection error:"<<ex.what()<<endl;
                        return 1;
                }
                
                return 0;
}

Connection * mConnectionPool::getConnection()
{
        Connection *conn;
        
        try{
                conn=connPool->createConnection(m_userName,m_userPassWord);
        }
        catch(SQLException &exp)
        {
                cout<<"get conn error:"<<exp.what()<<endl;
                return NULL;
        }
        return conn;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值