123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- #ifndef __SAFE_LIST__
- #define __SAFE_LIST__
- #include <list>
- #include <locale.h>
- #include "CritSection.h"
- class CDatabase;
- using namespace std;
- extern void WriteTextLog(const TCHAR *format, ...);
- template <class _Ty,class _Ax = allocator<_Ty>>
- class SafeList: public std::list<_Ty, _Ax>
- {
- public:
- ThreadSection _critSection;
- void _push_back(const _Ty& _Val)
- {
- AutoThreadSection aSection(&_critSection);
- push_back(_Val);
- }
- void _push_front(const _Ty& _Val)
- {
- AutoThreadSection aSection(&_critSection);
- push_front(_Val);
- }
- void _pop_front()
- {
- AutoThreadSection aSection(&_critSection);
- pop_front();
- }
- CDatabase* _pop_front_()
- {
- AutoThreadSection aSection(&_critSection);
- CDatabase* it = front();
- pop_front();
- return it;
- }
- void _pop_back()
- {
- AutoThreadSection aSection(&_critSection);
- pop_back();
- }
- void _remove(const _Ty& _Val_arg)
- {
- AutoThreadSection aSection(&_critSection);
- remove(_Val_arg);
- }
- reference _front()
- {
- AutoThreadSection aSection(&_critSection);
- return (*begin());
- }
- const_reference _front() const
- {
- AutoThreadSection aSection(&_critSection);
- return (*begin());
- }
- reference _back()
- {
- AutoThreadSection aSection(&_critSection);
- return (*(--end()));
- }
- const_reference _back() const
- {
- AutoThreadSection aSection(&_critSection);
- return (*(--end()));
- }
- size_type _size()
- {
-
- return (_Mysize);
- }
- };
- typedef SafeList<CDatabase*> ODBCConnectList;
- class CODBCPool
- {
- ThreadSection m_critSection;
-
- ODBCConnectList m_listIdleConnections;
-
- ODBCConnectList m_listBusyConnections;
-
- INT m_nMaxCount;
- INT m_nMinCount;
-
- DWORD m_dwDBPort;
- CString m_strDBSource;
- CString m_strDBAccount;
- CString m_strPassWord;
- CString m_strDBName;
- BOOL m_bNeedExit;
- BOOL m_bNeedStop;
- BOOL m_bNeedConnection;
- public:
- CODBCPool(void) {
- m_nRef = 0;
- m_nObjRef = 0;
- m_nMaxCount = 0;
- m_nMinCount = 0;
- m_bNeedExit = FALSE;
- m_bNeedStop = FALSE;
- m_bNeedConnection = FALSE;
- };
- ~CODBCPool(void) {DestroyAllDBConnections();};
-
- static CODBCPool* GetInstance()
- {
- static CODBCPool* pInstance = NULL;
- if ( pInstance == NULL )
- pInstance = new CODBCPool;
- return pInstance;
- }
-
- DWORD 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') || !lpDBName || lpDBName[0] == _T('\0') )
- {
- return 0;
- }
- m_strDBSource = lpDBSource;
- m_dwDBPort = dwDBPort;
- m_strDBAccount = lpDBAccount;
- m_strPassWord = lpPassWord;
- m_strDBName = lpDBName;
- m_nMinCount = nMinConn;
- m_nMaxCount = nMaxConn;
- return IntiAllConnections();
- }
-
- void ReleasePool(){DestroyAllDBConnections();}
-
- void SetDBConnectionInfo(IN LPCTSTR lpDBSource, IN CONST DWORD &dwDBPort, IN LPCTSTR lpDBAccount, IN LPCTSTR lpPassWord, IN LPCTSTR lpDBName);
-
- INT IntiAllConnections()
- {
- DestroyAllDBConnections();
-
- int nCount = 0;
- CDatabase *pDatabase = NULL;
- for (int i = 0; i < m_nMinCount; i++)
- {
- pDatabase = InitAConnection();
- if ( pDatabase )
- {
- nCount++;
- m_listIdleConnections._push_back(pDatabase);
- InterlockedIncrement(&m_nObjRef);
- pDatabase = NULL;
- }
- }
- return nCount;
- }
-
- void DestroyAllDBConnections()
- {
- m_bNeedExit = TRUE;
-
- 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);
- }
- itIdle = m_listIdleConnections.erase(itIdle);
- }
- m_bNeedExit = FALSE;
- }
-
- CDatabase* GetAConnection( IN CONST DWORD &dwTimeOut = 1000 )
- {
- if ( m_bNeedExit )
- return NULL;
-
- BOOL bGetIdl = FALSE;
- DWORD dwTime = GetTickCount();
- CDatabase* pDBEngine = NULL;
- do
- {
- if ( m_listIdleConnections._size() > 0){
-
- pDBEngine = m_listIdleConnections._pop_front_();
- if (pDBEngine)
- {
- m_listBusyConnections._push_back(pDBEngine);
- bGetIdl = TRUE;
- }
- }
- else
- {
- if (m_nObjRef < m_nMaxCount)
- {
-
- pDBEngine = InitAConnection();
- if (pDBEngine){
-
- m_listBusyConnections._push_back(pDBEngine);
- InterlockedIncrement(&m_nObjRef);
- WriteTextLog(_T("创建连接对象:共有 %d 个"), m_nObjRef);
- break;
- }
- }
- }
- if ( !bGetIdl ) {
-
- Sleep(0);
-
- if ( (GetTickCount() - dwTime) >= dwTimeOut){
- WriteTextLog(_T("获取连接对象超时"));
- break;
- }
- }
- } while ( !bGetIdl );
- return pDBEngine;
- }
-
- void RestoreAConnection(IN CDatabase *pDBEngine)
- {
- if ( pDBEngine != NULL ){
- m_listBusyConnections._remove(pDBEngine);
- m_listIdleConnections._push_back(pDBEngine);
- }
- }
- private:
- volatile LONG m_nRef;
- volatile LONG m_nObjRef;
-
- CDatabase* InitAConnection()
- {
- if ( m_nObjRef == m_nMaxCount )
- return NULL;
- CDatabase * pDBEngine = new CDatabase;
- TCHAR szConnString[MAX_PATH] = {0};
- if (m_dwDBPort != 0)
- {
- if ( m_strDBAccount.IsEmpty() )
- _stprintf_s(szConnString, MAX_PATH, DB_SW_CONN_WITH_PORT, m_strDBSource.GetString(), m_dwDBPort, m_strDBName.GetString());
- else
- _stprintf_s(szConnString, MAX_PATH, DB_SS_CONN_WITH_PORT, m_strDBSource.GetString(), m_dwDBPort, m_strDBName.GetString(), m_strDBAccount.GetString(), m_strPassWord.GetString());
- }
- else
- {
- if ( m_strDBAccount.IsEmpty() )
- _stprintf_s(szConnString, MAX_PATH, DB_SW_CONN_WITHOUT_PORT, m_strDBSource.GetString(), m_strDBName.GetString());
- else
- _stprintf_s(szConnString, MAX_PATH, DB_SS_CONN_WITHOUT_PORT, m_strDBSource.GetString(), m_strDBName.GetString(), m_strDBAccount.GetString(), m_strPassWord.GetString());
- }
- try
- {
- pDBEngine->OpenEx(szConnString, CDatabase::noOdbcDialog);
- }
- catch (CDBException* e)
- {
- if ( pDBEngine )
- delete pDBEngine;
- pDBEngine = NULL;
- WriteTextLog(_T("SQL连接串:%s, 返回错误:%s"), szConnString, e->m_strError);
- }
- return pDBEngine;
- }
-
- void CloseAConnection(CDatabase* pDBEngine)
- {
- m_listIdleConnections._remove(pDBEngine);
- InterlockedDecrement(&m_nObjRef);
- if ( pDBEngine )
- delete pDBEngine;
- }
-
- friend class ODBCConnGuard;
- public:
-
-
- BOOL ExecuteSQL( IN LPCTSTR lpExcuteSQL, IN CONST DWORD &dwTimeOut = 30000 );
- };
- #endif
|