123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- #include "StdAfx.h"
- #include "ODBCPool.h"
- CODBCPool* CODBCPool::m_pInstance = NULL;
- CODBCPool::CODBCPool(void)
- {
- m_nRef = 0;
- m_nObjRef = 0;
- m_nMaxCount = 0;
- m_nMinCount = 0;
- m_bNeedExit = FALSE;
- m_bNeedStop = FALSE;
- m_bNeedConnection = FALSE;
- m_hWorkThread = NULL;
- // 线程控制;
- m_hHaveData = CreateEvent (NULL, TRUE, FALSE, _T("DataConnPool"));
- //InitializeCriticalSection(&m_csConnList);
- InitializeCriticalSection(&m_csIdleConnList);
- InitializeCriticalSection(&m_csBusyConnList);
- }
- CODBCPool::~CODBCPool(void)
- {
- DestroyAllDBConnections();
- if ( m_hHaveData )
- SetEvent( m_hHaveData );
- // 等待退出;
- if ( m_hWorkThread )
- {
- WaitForSingleObject(m_hWorkThread, INFINITE);
- CloseHandle(m_hWorkThread);
- }
- if ( m_hHaveData )
- CloseHandle(m_hHaveData);
- DeleteCriticalSection(&m_csIdleConnList);
- DeleteCriticalSection(&m_csBusyConnList);
- }
- CODBCPool* CODBCPool::GetInstance()
- {
- if ( m_pInstance == NULL )
- {
- m_pInstance = new CODBCPool;
- }
- return m_pInstance;
- }
- // 返回成功初始化的数据库对象数量;
- DWORD CODBCPool::InitializePool(IN LPCTSTR lpDBSource, IN CONST DWORD &dwDBPort, IN LPCTSTR lpDBAccount, IN LPCTSTR lpPassWord, IN LPCTSTR lpDBName, IN CONST INT &nMinConn /* = 1 */, IN CONST INT &nMaxConn /* = 5 */)
- {
- if ( !lpDBSource || lpDBSource[0] == _T('\0') ||
- !lpDBAccount || lpDBAccount[0] == _T('\0') ||
- !lpPassWord || lpPassWord[0] == _T('\0') ||
- !lpDBName || lpDBName[0] == _T('\0') )
- {
- return 0;
- }
- m_lpDBSource = lpDBSource;
- m_dwDBPort = dwDBPort;
- m_lpDBAccount = lpDBAccount;
- m_lpPassWord = lpPassWord;
- m_lpDBName = lpDBName;
- m_nMinCount = nMinConn;
- m_nMaxCount = nMaxConn;
-
- return IntiAllConnections();
- }
- INT CODBCPool::IntiAllConnections()
- {
- // 先七公里现有的数据;
- DestroyAllDBConnections();
- // 开始按照最小数量开始创建;
- int nCount = 0;
- for (int i = 0; i < m_nMinCount; i++)
- {
- nCount = InitAConnection();
- }
- // 创建一个工作线程,用来进行一些后台维护工作;
- //if (NULL == m_hWorkThread)
- //{
- // m_hWorkThread = CreateThread(NULL, NULL, WorkThread, (LPVOID)this, 0, NULL);
- //}
- return nCount;
- }
- void CODBCPool::DestroyAllDBConnections()
- {
- m_bNeedExit = TRUE;
- StopWorkThread();
- // 首先等待m_listBusyConnections.size() == 0;
- while(1)
- {
- if ( m_listBusyConnections.size() == 0 )
- break;
- Sleep(1000);
- }
- ODBCConnectList::iterator itIdle = m_listIdleConnections.begin();
- while (itIdle != m_listIdleConnections.end())
- {
- if (NULL != (*itIdle))
- {
- (*itIdle)->Close();
- delete (*itIdle);
- //#ifdef _DEBUG
- static int ncount = 0;
- printf("当前删除连接个数:【%d】\t\n",ncount++);
- //#endif
- }
- itIdle = m_listIdleConnections.erase(itIdle);
- }
- m_bNeedExit = FALSE;
- }
- int CODBCPool::InitAConnection()
- {
- EnterCriticalSection(&m_csIdleConnList);
- if ( m_nObjRef == m_nMaxCount )
- {
- LeaveCriticalSection(&m_csIdleConnList);
- return m_nMaxCount;
- }
-
- BOOL bSuccess = FALSE;
- CODBCConnect * pDBEngine = new CODBCConnect(m_lpDBSource, m_dwDBPort, m_lpDBAccount, m_lpPassWord, m_lpDBName, bSuccess);
- if (bSuccess)
- {
- InterlockedIncrement(&m_nObjRef); //增加引用计数;
- pDBEngine->SetObjId(m_nObjRef);
- m_listIdleConnections.push_back(pDBEngine);
- //#ifdef _DEBUG
- printf("当前连接池个数:%d\t\n",m_nObjRef);
- //#endif
- }
- else
- {
- if ( pDBEngine )
- delete pDBEngine;
- }
- LeaveCriticalSection(&m_csIdleConnList);
- return m_nObjRef;
- }
- void CODBCPool::CloseAConnection(CODBCConnect* pDBEngine)
- {
- // 从空闲队列将其删除;
- EnterCriticalSection(&m_csIdleConnList);
- m_listIdleConnections.remove(pDBEngine);
- InterlockedDecrement(&m_nObjRef); // 减少计数;
- LeaveCriticalSection(&m_csIdleConnList);
- //pDBEngine->Close();
- if ( pDBEngine )
- delete pDBEngine;
- }
- CODBCConnect * CODBCPool::GetAConnection( IN CONST DWORD &dwTimeOut )
- {
- EnterCriticalSection(&m_csIdleConnList);
- if ( m_bNeedExit )
- {// 停止获取;
- LeaveCriticalSection(&m_csIdleConnList);
- return NULL;
- }
- // 1.首先到池中查找有无空闲对象;
- BOOL bGetIdl = FALSE;
- DWORD dwTime = GetTickCount();
- CODBCConnect* pDBEngine = NULL;
- do
- {
- if ( m_listIdleConnections.size() > 1)
- {
- pDBEngine = m_listIdleConnections.front();
- m_listIdleConnections.pop_front();
- EnterCriticalSection(&m_csBusyConnList); // 防止同时push和pop;
- m_listBusyConnections.push_back(pDBEngine);
- LeaveCriticalSection(&m_csBusyConnList);
- bGetIdl = TRUE;
- }
- else
- {
- if ( (m_listBusyConnections.size() >= m_nMinCount - 1) && (m_nObjRef < m_nMaxCount))
- {
- BOOL bSuccess = FALSE;
- pDBEngine = new CODBCConnect(m_lpDBSource, m_dwDBPort, m_lpDBAccount, m_lpPassWord, m_lpDBName, bSuccess);
- if (bSuccess)
- {
- InterlockedIncrement(&m_nObjRef); //增加引用计数;
- EnterCriticalSection(&m_csBusyConnList); // 防止同时push和pop;
- m_listBusyConnections.push_back(pDBEngine);
- pDBEngine->SetObjId(m_nObjRef);
- LeaveCriticalSection(&m_csBusyConnList);
- //#ifdef _DEBUG
- printf("当前连接池个数:%d\t\n",m_nObjRef);
- //#endif
- break;
- }
- else
- {
- if ( pDBEngine )
- delete pDBEngine;
- }
- }
- }
- if ( !bGetIdl )
- {
- // 未找到,小憩一会,防止CPU爆满;
- Sleep(100);
- // 超时,则结束返回NULL;
- if ( (GetTickCount() - dwTime) >= dwTimeOut)
- {
- //#ifdef _DEBUG
- printf("----------------###【获取连接对象超时】###----------------\n");
- //#endif
- break;
- }
- }
- } while ( !bGetIdl );
- LeaveCriticalSection(&m_csIdleConnList);
- return pDBEngine;
- }
- int CODBCPool::RestoreAConnection(CODBCConnect* pDBEngine)
- {
- EnterCriticalSection(&m_csBusyConnList); // 防止同时push和pop;
- if ( pDBEngine != NULL )
- {
- m_listBusyConnections.remove(pDBEngine);
- // 此处不需要EnterCriticalSection(m_csIdleConnList),否则会锁死;
- m_listIdleConnections.push_back(pDBEngine);
- }
- LeaveCriticalSection(&m_csBusyConnList);
- return 0;
- }
- void CODBCPool::StopWorkThread()
- {
- if ( m_hWorkThread )
- {
- m_bNeedStop = TRUE;
- // 因为线程是无限制等待信号的,所以这里先把标志位置为停止,然后发信号让线程检测;
- SetEvent(m_hHaveData);
- // 等待退出;
- WaitForSingleObject(m_hWorkThread, INFINITE);
- CloseHandle(m_hWorkThread);
- m_hWorkThread = NULL;
- CloseHandle(m_hHaveData);
- m_hHaveData = NULL;
- }
- }
- BOOL CODBCPool::IsNeedStop()
- {
- return m_bNeedStop;
- }
- BOOL CODBCPool::IsNeedConnection()
- {
- return m_bNeedConnection;
- }
- /************************************************************************/
- /* 维护线程; */
- /************************************************************************/
- DWORD WINAPI CODBCPool::WorkThread( LPVOID pdata)
- {
- CODBCPool * pConPool = (CODBCPool *) pdata;
- do
- {
- // 清理空闲连接对象;
- EnterCriticalSection(&pConPool->m_csIdleConnList);
- if ( pConPool->m_listBusyConnections.size() > 0 && pConPool->m_listBusyConnections.size() < pConPool->m_nMinCount )
- {
- if ( pConPool->m_listIdleConnections.size() == pConPool->m_nMinCount )
- {
- LeaveCriticalSection(&pConPool->m_csIdleConnList);
- continue;
- }
- // 从后面开始删除;
- CODBCConnect *pDBEngine = pConPool->m_listIdleConnections.back();
- pConPool->m_listIdleConnections.pop_back();
- //pDBEngine->Close();
- if ( pDBEngine)
- {
- //printf("删除连接对象成功%d\n",pConPool->m_nObjRef);
- delete pDBEngine;
- }
- InterlockedDecrement(&pConPool->m_nObjRef); // 减少计数;
- }
- LeaveCriticalSection(&pConPool->m_csIdleConnList);
- Sleep(500); // 降低CPU使用率;
- } while (WaitForSingleObject(pConPool->m_hHaveData, 500) == WAIT_TIMEOUT );
- return 0;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // 客户端调用的主接口;
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- BOOL CODBCPool::GetSelectby(IN LPCTSTR lpTblName, IN LPCTSTR lpColumn, IN LPCTSTR lpFilters, OUT LPTSTR lpResult, IN CONST INT& nBufLen, IN CONST DWORD &dwTimeOut)
- {
- CODBCConnect *pDBConn = NULL;
- ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
- //此处为实际实现的接口,自己需要实现。
- if ( pDBConn == NULL)
- {
- return FALSE;
- }
- return pDBConn->GetSelectby(lpTblName, lpColumn, lpFilters, lpResult, nBufLen);
- }
- BOOL CODBCPool::ExecuteSQL( IN LPCTSTR lpExcuteSQL, IN CONST DWORD &dwTimeOut )
- {
- CODBCConnect *pDBConn = NULL;
- ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
- //此处为实际实现的接口,自己需要实现。
- if ( pDBConn == NULL)
- {
- return FALSE;
- }
- return pDBConn->ExecuteSQL(lpExcuteSQL);
- }
- INT CODBCPool::GetTblRecordCount( IN LPCTSTR lpTblName, IN LPCTSTR lpFilters, IN CONST DWORD &dwTimeOut )
- {
- CODBCConnect *pDBConn = NULL;
- ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
- if ( pDBConn == NULL)
- {
- return FALSE;
- }
- //此处为实际实现的接口,自己需要实现。
- return pDBConn->GetTblRecordCount(lpTblName, lpFilters);
- }
- BOOL CODBCPool::GetVersionInfo( OUT LPVOID lpVerInfo, IN CONST DWORD &dwTimeOut )
- {
- CODBCConnect *pDBConn = NULL;
- ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
- if ( pDBConn == NULL)
- {
- return FALSE;
- }
- //此处为实际实现的接口,自己需要实现。
- return pDBConn->GetVersionInfo(lpVerInfo);
- }
- BOOL CODBCPool::GetNetShareInfo( OUT LPVOID lpNetShareInfo, IN CONST DWORD &dwTimeOut )
- {
- CODBCConnect *pDBConn = NULL;
- ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
- if ( pDBConn == NULL)
- {
- return FALSE;
- }
- //此处为实际实现的接口,自己需要实现。
- return pDBConn->GetNetShareInfo(lpNetShareInfo);
- }
- BOOL CODBCPool::GetOrderInfo( OUT LPVOID lpOrderInfo, IN CONST DWORD &dwTimeOut )
- {
- //printf("\t\n【创建新守卫对象】\n");
- CODBCConnect *pDBConn = NULL;
- ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
- if ( pDBConn == NULL)
- {
- return FALSE;
- }
- //此处为实际实现的接口,自己需要实现。
- return pDBConn->GetOrderInfo(lpOrderInfo);
- }
- BOOL CODBCPool::GetunauthInfo( IN LPCTSTR lpSQLFilters, OUT LPTSTR lpUnauthInfo, IN CONST DWORD &dwTimeOut )
- {
- //printf("\t\n【创建新守卫对象】\n");
- CODBCConnect *pDBConn = NULL;
- ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
- if ( pDBConn == NULL)
- {
- return FALSE;
- }
- //此处为实际实现的接口,自己需要实现。
- return pDBConn->GetunauthInfo(lpSQLFilters, lpUnauthInfo);
- }
- BOOL CODBCPool::GetunauthInfo( IN LPCTSTR lpSQLFilters, OUT TString &strUnauthInfo, IN CONST DWORD &dwTimeOut )
- {
- //printf("\t\n【创建新守卫对象】\n");
- CODBCConnect *pDBConn = NULL;
- ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
- if ( pDBConn == NULL)
- {
- return FALSE;
- }
- //此处为实际实现的接口,自己需要实现。
- return pDBConn->GetunauthInfo(lpSQLFilters, strUnauthInfo);
- }
- INT CODBCPool::GetClientEnterpriseName( IN LPCTSTR lpSQLFilters, OUT LPVOID lpClientIPInfo, IN CONST DWORD &dwTimeOut )
- {
- CODBCConnect *pDBConn = NULL;
- ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
- if ( pDBConn == NULL)
- {
- return FALSE;
- }
- //此处为实际实现的接口,自己需要实现。
- return pDBConn->GetClientEnterpriseName(lpSQLFilters, lpClientIPInfo);
- }
- INT CODBCPool::GetEnterprisInfo(IN LPCTSTR lpEnterpriseName, OUT CArray<CStringArray, CStringArray>& AryEnterpriseInfo, IN CONST DWORD &dwTimeOut /* = 30000 */)
- {
- CODBCConnect *pDBConn = NULL;
- ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
- if ( pDBConn == NULL)
- {
- return FALSE;
- }
- //此处为实际实现的接口,自己需要实现。
- return pDBConn->GetEnterprisInfo(lpEnterpriseName, AryEnterpriseInfo);
- }
- BOOL CODBCPool::GetDBCFileInfo(IN LPCTSTR lpSQL, OUT TString& strResult, IN CONST DWORD &dwTimeOut)
- {
- CODBCConnect *pDBConn = NULL;
- ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
- if ( pDBConn == NULL)
- {
- return FALSE;
- }
- return pDBConn->GetDBCFileInfo(lpSQL, strResult);
- }
- BOOL CODBCPool::GetTookOrderInfo( OUT LPVOID lpTookOrderInfo, IN CONST DWORD &dwTimeOut /* = 3000 */ )
- {
- CODBCConnect *pDBConn = NULL;
- ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
- if ( pDBConn == NULL)
- {
- return FALSE;
- }
- //此处为实际实现的接口,自己需要实现。
- return pDBConn->GetTookOrderInfo(lpTookOrderInfo);
- }
|