#include "stdafx.h"
#include "ocilib.h"
#include<string>
#include<string.h>
#include<stdlib.h>
#include <iostream>
#include <fstream>
#include <time.h>
#include <sstream>
using namespace std;
#pragma comment(lib,"ociliba.lib")
#pragma comment(lib,"ocilibm.lib")
#pragma comment(lib,"ocilibw.lib")
#define MAX_THREADS 10
#define MAX_CONN 10
#define SIZE_STR 260
void worker(OCI_Thread *thread, void *data)
{
OCI_Connection *cn = (OCI_Connection *)OCI_PoolGetConnection((OCI_ConnPool*)data, NULL);
char str[SIZE_STR+1];
std::cout<<"#######"<<cn<<std::endl;
/* application work here */
str[0] = 0;
OCI_Immediate(cn, "select to_char(sysdate, 'YYYYMMDD HH24:MI:SS') from dual", OCI_ARG_TEXT, str);
printf("%s\n", str);
/* ... */
OCI_ConnectionFree(cn);
}
int main(void)
{
OCI_Thread *th[MAX_THREADS];
OCI_ConnPool *pool;
if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT | OCI_ENV_THREADED))
return EXIT_FAILURE;
/* create pool */
pool = OCI_PoolCreate("T1", "chf", "chf", OCI_POOL_CONNECTION, OCI_SESSION_DEFAULT, 0, MAX_CONN, 1);
/* create threads */
for (int i = 0; i < MAX_THREADS; i++)
{
OCI_Connection *cn = (OCI_Connection *)OCI_PoolGetConnection((OCI_ConnPool*)pool, NULL);
if(cn)
{
std::cout<<" the current busy cnt : "<<cn<<" "<<OCI_PoolGetBusyCount(pool)<<" "<<OCI_PoolGetOpenedCount(pool)<<" "<<
OCI_PoolGetMax(pool)<<"---"<<OCI_GetDatabase(cn)<<std::endl
<<OCI_GetInstanceName(cn)<<std::endl
<<OCI_GetServiceName(cn)<<std::endl
<<OCI_GetServerName(cn)<<std::endl;
OCI_ConnectionFree(cn);
}
}
std::cout<<"-------------------------------------"<<std::endl;
OCI_PoolFree(pool);
OCI_Cleanup();
getchar();
return EXIT_SUCCESS;
}
小结如下:
1.oci连接池,总是反复利用已经创建的连接,除非连接不够用才会创建新连接
2.OCI连接池支持ORACLE RAC模式,并且连接的时候,会从rac的配置文件中,依次读取配置ip等信息,进行连接测试,直到找到可用的IP/域名等信息为止