- .h文件:
- #ifndef __DBCONNECTION_H__
- #define __DBCONNECTION_H__
- #include <occi.h>
- #include <pthread.h>
- #include <string>
- #include <queue>
- #include <errno.h>
- #include <vector>
- #include "time.h"
- #include<sys/time.h>
- using namespace std;
- using namespace oracle;
- using namespace oracle::occi;
- typedef struct
- {
- int iParamPos;//参数的位置
- int iParamStyle;//参数类型 0字符 1数字
- int iInOrOut;//0传入 1传出
- string sParamValue;//字符参数值
- int iParamValue;//数值参数值
- int iParamValueLen;//参数长度
- }SFunc;
- #define MAX_CONNECTION 10 // 100
- #define TIMED_SECOND 120 // 120
- #define MAX_AVAILABILITY 0x7FFFFFFF
- const long gc_Diff_1970_2000 = 946684800L;
- class DBLock
- {
- public:
- DBLock();
- ~DBLock();
- void Lock();
- void Unlock();
- void TimedWait(int second); //second<0无限等;
- void Signal();
- void Broadcast();
- private:
- pthread_mutex_t m;
- pthread_cond_t cond;
- };
- class DBLockGuard
- {
- public:
- DBLockGuard(DBLock *l);
- ~DBLockGuard();
- private:
- DBLock *m_l;
- };
- //-------------------------------------------------------------
- class DBConectionPool;
- class DBConnection
- {
- public:
- DBConnection();
- ~DBConnection();
- bool Connect();
- void Disconnect();
- ResultSet* ExecuteSql(const char *szSql);
- ResultSet* ExecuteSql2(const char *szSql);
- int Execute(const char *szSql);
- int Execute2(const char *szSql);
- int ExecuteFun(const char *szSql,vector<SFunc> * vInparam,vector<SFunc> * vOutParam);
- int ExecuteFun2(const char*func,const char*para1,string& sRet);
- void COMMIT();
- void ROLLBACK();
- void CloseRecordSet(ResultSet *pResultSet);
- void SetTimeout(bool bTimeout);
- bool IsTimeout();
- friend class DBConnectionPool;
- Statement *stmt2;
- private:
- Environment *env;
- Connection * conn;
- //Connection *m_conn;
- Statement *stmt;
- bool m_bTimeout;
- //ConnectionPtr m_conn;
- };
- class DBConectionPool
- {
- public:
- DBConectionPool();
- ~DBConectionPool();
- static DBConectionPool* GetInstance();
- bool GetConnect(DBConnection** conn);
- void ReleaseConnect(DBConnection* conn);
- private:
- static DBConectionPool * m_dbpool;
- DBConectionPool(const DBConectionPool& rhs);
- DBConectionPool& operator=(const DBConectionPool& rhs);
- void Init();
- void CloseAll();
- queue<DBConnection*> m_connpool;
- DBLock m_l;
- };
- class DBConnectionGuard
- {
- public:
- DBConnectionGuard(DBConnection *db);
- ~DBConnectionGuard();
- private:
- DBConnection *m_db;
- };
- extern DBConectionPool * g_pDBConnectionPool;
- class DBDate
- {
- public:
- static void getDateFromInt(const char * strDate, std::string &strDateRe);
- };
- #endif
- .cpp文件:
- #include <iostream>
- #include "dbconnection.hpp"
- #include <string>
- #include <time.h>
- using namespace std;
- DBConectionPool * g_pDBConnectionPool = NULL;
- DBLock::DBLock()
- {
- pthread_mutex_init(&m,NULL);
- pthread_cond_init(&cond, NULL);
- }
- DBLock::~DBLock()
- {
- Broadcast();
- pthread_cond_destroy(&cond);
- pthread_mutex_destroy(&m);
- }
- void DBLock::Lock()
- {
- pthread_mutex_lock(&m);
- }
- void DBLock::Unlock()
- {
- pthread_mutex_unlock(&m);
- }
- void DBLock::TimedWait(int second)
- {
- if (second <= 0) {
- pthread_cond_wait(&cond, &m);
- } else {
- struct timeval now;
- struct timespec timeout;
- gettimeofday(&now, NULL);
- timeout.tv_sec = now.tv_sec + second;
- timeout.tv_nsec = now.tv_usec * 1000;
- int retcode = 0;
- retcode = pthread_cond_timedwait(&cond, &m, &timeout);
- if (retcode == ETIMEDOUT) {
- cout<<"[DBLOCK TIMED WAIT]Timed out\\n";
- }
- }
- }
- void DBLock::Signal()
- {
- pthread_cond_signal(&cond);
- }
- void DBLock::Broadcast()
- {
- pthread_cond_broadcast(&cond);
- }
- //////////////////////////////////////////////////////////////////////////
- // DBLockGuard
- DBLockGuard::DBLockGuard(DBLock *l)
- : m_l(l)
- {
- m_l->Lock();
- }
- DBLockGuard::~DBLockGuard()
- {
- m_l->Unlock();
- }
- //////////////////////////////////////////////////////////////////////////
- // DBConnection
- DBConnection::DBConnection(): conn(0L), m_bTimeout(false)
- {
- env = NULL;
- conn = NULL;
- stmt = NULL;
- stmt2 = NULL;
- Connect();
- }
- DBConnection::~DBConnection()
- {
- Disconnect();
- }
- bool DBConnection::Connect()
- {
- try
- {
- bool bRet = false;
- env = Environment::createEnvironment((Environment::Mode((Environment::OBJECT|Environment::THREADED_MUTEXED)))) ;
- if (env == NULL)
- {
- return bRet;
- }
- conn = env->createConnection(“你的oracle帐号”,“你的oracle密码”,“你的oracle连接地址”);
- if (conn == NULL)
- {
- Environment::terminateEnvironment(env);
- return bRet;
- }
- stmt = conn->createStatement();
- stmt2 = conn->createStatement();
- stmt->setAutoCommit(false);
- stmt2->setAutoCommit(false);
- if ( (stmt == NULL) || (stmt2 == NULL) )
- {
- env->terminateConnection(conn);
- Environment::terminateEnvironment(env);
- return bRet;
- }
- }
- catch(SQLException &ex)
- {
- return false;
- }
- return true ;
- }
- void DBConnection::Disconnect()
- {
- try
- {
- if (stmt)
- {
- conn->terminateStatement(stmt);
- stmt = NULL;
- }
- if (stmt2)
- {
- conn->terminateStatement(stmt2);
- stmt2 = NULL;
- }
- if (conn)
- {
- env->terminateConnection(conn);
- conn = NULL;
- }
- if (env)
- {
- Environment::terminateEnvironment(env);
- env = NULL;
- }
- }
- catch(SQLException &ex)
- {
- }
- }
- ResultSet* DBConnection::ExecuteSql(const char *szSql)
- {
- if (stmt == NULL)
- {
- return NULL;
- }
- stmt->setSQL(szSql);
- return stmt->executeQuery(szSql);
- }
- ResultSet* DBConnection::ExecuteSql2(const char *szSql)
- {
- if (stmt2 == NULL)
- {
- return NULL;
- }
- stmt2->setSQL(szSql);
- return stmt2->executeQuery(szSql);
- }
- int DBConnection::Execute(const char *szSql)
- {
- if (stmt == NULL)
- {
- return -1;
- }
- stmt->setSQL(szSql);
- stmt->execute(szSql);
- return 0;
- }
- int DBConnection::Execute2(const char *szSql)
- {
- if (stmt2 == NULL)
- {
- return -1;
- }
- stmt2->setSQL(szSql);
- stmt2->execute(szSql);
- return 0;
- }
- void DBConnection::CloseRecordSet(ResultSet *pResultSet)
- {
- if (stmt == NULL)
- {
- return;
- }
- stmt->closeResultSet(pResultSet);
- }
- void DBConnection::COMMIT()
- {
- conn->commit();
- }
- void DBConnection::ROLLBACK()
- {
- conn->rollback();
- }
- void DBConnection::SetTimeout(bool bTimeout)
- {
- m_bTimeout = bTimeout;
- }
- bool DBConnection::IsTimeout()
- {
- return m_bTimeout;
- }
- //////////////////////////////////////////////////////////////////////////
- // DBConnectionPool
- DBConectionPool * DBConectionPool::m_dbpool = NULL;
- DBConectionPool::DBConectionPool()
- {
- Init();
- }
- DBConectionPool::~DBConectionPool()
- {
- CloseAll();
- }
- DBConectionPool* DBConectionPool::GetInstance()
- {
- if (m_dbpool == NULL)
- {
- m_dbpool = new DBConectionPool();
- }
- return m_dbpool;
- }
- bool DBConectionPool::GetConnect(DBConnection** conn)
- {
- DBLockGuard l(&m_l);
- while (m_connpool.empty())
- {
- m_l.TimedWait(TIMED_SECOND);
- }
- *conn = m_connpool.front();
- if ((*conn)->IsTimeout())
- {
- (*conn)->Connect();
- (*conn)->SetTimeout(false);
- }
- m_connpool.pop();
- return true;
- }
- void DBConectionPool::ReleaseConnect(DBConnection* conn)
- {
- DBLockGuard l(&m_l);
- m_connpool.push(conn);
- if (m_connpool.size() == 1)
- {
- m_l.Signal();
- }
- }
- void DBConectionPool::Init()
- {
- for (int index = 0; index < CBSParam::m_iMaxConn; ++index)
- {
- DBConnection *conn = new DBConnection();
- if( conn->Connect() )
- {
- cout<<"connect success"<<endl;
- m_connpool.push(conn);
- }
- else
- {
- cout<<"connect failed"<<endl;
- delete conn;
- }
- }
- cout<<"connection pool size is : "<<m_connpool.size()<<endl;
- }
- void DBConectionPool::CloseAll()
- {
- while (!m_connpool.empty())
- {
- DBConnection *conn = m_connpool.front();
- m_connpool.pop();
- delete conn;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- // DBConnectionGuard
- DBConnectionGuard::DBConnectionGuard(DBConnection *db)
- : m_db(db)
- {
- }
- DBConnectionGuard::~DBConnectionGuard()
- {
- if (m_db!=NULL)
- g_pDBConnectionPool->ReleaseConnect(m_db);
- }
- //////////////////////////////////////////////////////////////////////////
- // DBDate
- void DBDate::getDateFromInt(const char * strDate, std::string &strDateRe)
- {
- struct tm *local, Tm;
- long itime = atoi( strDate ) + gc_Diff_1970_2000 ;
- local = localtime_r(&itime, &Tm);
- char chDate [12];
- snprintf(chDate, sizeof(chDate),"%d%02d%02d",local->tm_year+1900 ,local->tm_mon+1,local->tm_mday);
- strDateRe = chDate;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1704201512291.html
来源: http://www.codesnippet.cn/detail/1704201512291.html