123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #ifndef __ODBC_POOL_20151016__
- #define __ODBC_POOL_20151016__
- #pragma once
- #include "ODBCConnect.h"
- class CODBCPool
- {
- public:
- CODBCPool(void);
- ~CODBCPool(void);
- // 获取实例指针;
- static CODBCPool* GetInstance();
- // 初始化所有连接;
- 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(){DestroyAllDBConnections();}
- // 设置数据库信息;
- void SetDBConnectionInfo(IN LPCTSTR lpDBSource, IN CONST DWORD &dwDBPort, IN LPCTSTR lpDBAccount, IN LPCTSTR lpPassWord, IN LPCTSTR lpDBName);
- // 初始化所有连接;
- INT IntiAllConnections();
- // 断开所有连接;
- void DestroyAllDBConnections();
- // 获取一个空闲连接;
- CODBCConnect* GetAConnection( IN CONST DWORD &dwTimeOut = 1000 );
- // 交还连接给空闲队列;
- INT RestoreAConnection(IN CODBCConnect *pDBConn);
- private:
- volatile LONG m_nRef;
- volatile LONG m_nObjRef;
- // 创建一个连接;
- INT InitAConnection();
- // 关闭一个连接;
- void CloseAConnection(CODBCConnect* pDBEngine);
- // 停止工作者线程;
- void StopWorkThread();
- // 判断是否需要停止工作;
- BOOL IsNeedStop();
- // 判断是否需要连接;
- BOOL IsNeedConnection();
-
- // 将守卫类友元化;
- friend class ODBCConnGuard;
- // 单例实例;
- static CODBCPool* m_pInstance;
- // 空闲数据库连接池;
- ODBCConnectList m_listIdleConnections;
- // 在使用的数据库连接池;
- ODBCConnectList m_listBusyConnections;
- // 保护连接池的临界区
- CRITICAL_SECTION m_csIdleConnList;
- CRITICAL_SECTION m_csBusyConnList;
- // 可用的指标:最大、最小;
- INT m_nMaxCount;
- INT m_nMinCount;
- // 引入的指针变量;
- LPCTSTR m_lpDBSource;
- DWORD m_dwDBPort;
- LPCTSTR m_lpDBAccount;
- LPCTSTR m_lpPassWord;
- LPCTSTR m_lpDBName;
- // 维护线程;
- HANDLE m_hWorkThread;
- HANDLE m_hHaveData;
- BOOL m_bNeedExit; // 退出数据池;
- BOOL m_bNeedStop;
- BOOL m_bNeedConnection;
- static DWORD WINAPI WorkThread( IN LPVOID lpInput );
- public:
- BOOL GetSelectby(IN LPCTSTR lpTblName, IN LPCTSTR lpColumn, IN LPCTSTR lpFilters, OUT LPTSTR lpResult, IN CONST INT& nBufLen, IN CONST DWORD &dwTimeOut = 3000 );
- //////////////////////////////////////////////////////////////////////////
- // 数据库接口;
- BOOL ExecuteSQL( IN LPCTSTR lpExcuteSQL, IN CONST DWORD &dwTimeOut = 30000 );
- INT GetTblRecordCount( IN LPCTSTR lpTblName, IN LPCTSTR lpFilters, IN CONST DWORD &dwTimeOut = 30000 );
- //////////////////////////////////////////////////////////////////////////
- // 公司域名接收后台;
- BOOL GetunauthInfo( IN LPCTSTR lpSQLFilters, OUT LPTSTR lpUnauthInfo, IN CONST DWORD &dwTimeOut = 30000 );
- BOOL GetunauthInfo( IN LPCTSTR lpSQLFilters, OUT TString &strUnauthInfo, IN CONST DWORD &dwTimeOut = 30000 );
- INT GetClientEnterpriseName( IN LPCTSTR lpSQLFilters, OUT LPVOID lpClientIPInfo, IN CONST DWORD &dwTimeOut = 30000 );
- INT GetEnterprisInfo(IN LPCTSTR lpEnterpriseName, OUT CArray<CStringArray, CStringArray>& AryEnterpriseInfo, IN CONST DWORD &dwTimeOut = 30000);
- BOOL GetDBCFileInfo(IN LPCTSTR lpSQL, OUT TString& strResult, IN CONST DWORD &dwTimeOut = 3000);
- BOOL GetVersionInfo( OUT LPVOID lpVerInfo, IN CONST DWORD &dwTimeOut = 30000 );
- BOOL GetNetShareInfo( OUT LPVOID lpNetShareInfo, IN CONST DWORD &dwTimeOut = 30000 );
- BOOL GetOrderInfo( OUT LPVOID lpOrderInfo, IN CONST DWORD &dwTimeOut = 30000 );
- BOOL GetTookOrderInfo( OUT LPVOID lpTookOrderInfo, IN CONST DWORD &dwTimeOut = 3000 );
- };
- // ODBC守卫垫片类;
- class ODBCConnGuard
- {
- CODBCConnect *m_pODBCConn;
- public:
- ODBCConnGuard( CODBCConnect *&pDBConn, CONST DWORD &dwTimeOut = 30000 )
- {
- pDBConn = CODBCPool::GetInstance()->GetAConnection( dwTimeOut );
- m_pODBCConn = pDBConn;
- }
- virtual ~ODBCConnGuard()
- {
- CODBCPool::GetInstance()->RestoreAConnection(m_pODBCConn);
- }
- };
- #endif
|