123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #ifndef __ODBC_POOL__
- #define __ODBC_POOL__
- #pragma once
- #include "SafeList.h"
- #include <afxdb.h>
- 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) ;
- ~CODBCPool(void);
-
- 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
- );
-
- void ReleasePool();
-
- void SetDBConnectionInfo(IN LPCTSTR lpDBSource, IN CONST DWORD &dwDBPort, IN LPCTSTR lpDBAccount, IN LPCTSTR lpPassWord, IN LPCTSTR lpDBName);
-
- INT IntiAllConnections();
-
- void DestroyAllDBConnections();
-
- CDatabase* GetAConnection( IN CONST DWORD &dwTimeOut = 1000 );
-
- void RestoreAConnection(IN CDatabase *pDBEngine);
-
- void CloseAConnection(CDatabase *&pDBEngine);
-
- void CloseBusyConnection(IN CDatabase *&pDataBase);
- private:
- volatile LONG m_nRef;
- volatile LONG m_nObjRef;
-
- CDatabase* InitAConnection();
-
- friend class ODBCConnGuard;
- };
- class ODBCConnGuard
- {
- CDatabase *m_pODBCConn;
- public:
- ODBCConnGuard( CDatabase *&pDBConn, CONST DWORD &dwTimeOut = 30000 ):m_pODBCConn(NULL)
- {
- pDBConn = CODBCPool::GetInstance()->GetAConnection( dwTimeOut );
- m_pODBCConn = pDBConn;
- }
- virtual ~ODBCConnGuard()
- {
- CODBCPool::GetInstance()->RestoreAConnection(m_pODBCConn);
- }
- };
- #endif
|