C++使用OCCI连接

在OCCI程序中使能线程安全,则应指定THREADED_MUTEXED选项创建运行环境
例如:
Environment *env = Environment::createEnvironment(Environment::THREADED_MUTEXED)

注意:
如果应用程序是单线程,则使用默认值创建环境即可;
如果指定THREADED_MUTEXED选项创建将会影响应用程序性能。
OCCI中的线程安全仅仅指的是Environment、Map、ConnectionPool、
StatelessConnectionPool和Connection对象。对于Statement、ResultSet、
SQLException、Stream等不是线程安全的,因此不应该在多个线程中共享。

#include
#include

#include <pthread.h>
#include <unistd.h>
#include <occi.h>

#define USERNAME “scott”
#define PASSWORD “scott”
#define DBNAME “//192.168.42.135:1521/orcl”
#define MAXCON 1
#define MINCON 0
#define INCCON 1

using namespace oracle::occi;
using namespace std;

void *displayAllRows(void *arg);
void *updateRow(void *arg);

int main(void)
{
Environment *env = Environment::createEnvironment(Environment::THREADED_MUTEXED);
//env = oc::Environment::createEnvironment(oc::Environment::DEFAULT);
//创建无状态连接池
StatelessConnectionPool *scp =
env->createStatelessConnectionPool(
USERNAME, PASSWORD, DBNAME, MAXCON, MINCON, INCCON,
StatelessConnectionPool::HOMOGENEOUS);

//设置池中的连接空闲超时时间,超时后OCCI会自动释放此连接,需要时在创建
scp->setTimeOut(10);


cout << "*****************Information**************************" << endl;
cout << "Open Connection : " << scp->getOpenConnections() << endl;
cout << "Busy Connection : " << scp->getBusyConnections() << endl;
cout << "Time Out Connection : " << scp->getTimeOut() << endl;
cout << "******************************************************" << endl;


pthread_t tid_1, tid_2;
int err = 0;
err = pthread_create(&tid_1, NULL, displayAllRows, (void *)scp);
if (err != 0) {
    cout << "create thread failure for tid_1." << endl;
} else {
    cout << ">> create thread successful for tid_1." << endl;
}

err = pthread_create(&tid_2, NULL, updateRow, (void *)scp);
if (err != 0) {
    cout << "create thread failure for tid_2." << endl;
} else {
    cout << ">> create thread successful for tid_2." << endl;
}

pthread_join(tid_1, (void **)NULL);
pthread_join(tid_2, (void **)NULL);


env->terminateStatelessConnectionPool(scp);
Environment::terminateEnvironment(env);

return 0;

}

void *displayAllRows(void *arg)
{
StatelessConnectionPool *scp = (StatelessConnectionPool *)arg;

for ( ; ; )
{
    std::cout << ">>>> displayAllRows thread runing [" << long(pthread_self()) << "]" << std::endl;

    Connection *conn = scp->getConnection("");
    
    string sqlStmt = "SELECT * FROM DEPT WHERE DEPTNO=60";
    
    Statement *stmt = conn->createStatement(sqlStmt);
    
    ResultSet *rset = stmt->executeQuery();

    try {
        while (rset->next()) {
            cout << int(rset->getNumber(1)) << "    ";
            cout << rset->getString(2) << " ";
            cout << rset->getString(3) << " ";
            cout << endl;
        }
    }catch(SQLException &ex) {
        cout << "display all rows failure." << endl;
        cout << ex.getMessage();
        cout << endl;
    }

    sleep(5);

    stmt->closeResultSet(rset);
    conn->terminateStatement(stmt);
    scp->releaseConnection(conn, "");
}

pthread_exit((void *)0);

}

void *updateRow(void *arg)
{
StatelessConnectionPool scp = (StatelessConnectionPool)arg;

for ( ; ; )
{
    std::cout << ">>> updateRow thread running [" << long(pthread_self()) << "]" << std::endl;

    Connection *conn = scp->getConnection("");
    
    string sqlStmt = "UPDATE DEPT SET LOC=:deptLocal WHERE DEPTNO=51";

    Statement *stmt = conn->createStatement(sqlStmt);

    try {
        stmt->setString(1, "ShangHai");
        stmt->executeUpdate();
        conn->commit();
    } catch(SQLException &ex) {
        cout << "update row failure." << endl;
        cout << ex.getMessage();
        cout << endl;
    }

    conn->terminateStatement(stmt);
    scp->releaseConnection(conn, "");

    sleep(1);
}

pthread_exit((void *)0);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值