ODBCPool.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef __ODBC_POOL_20151016__
  2. #define __ODBC_POOL_20151016__
  3. #pragma once
  4. #include "ODBCConnect.h"
  5. class CODBCPool
  6. {
  7. public:
  8. CODBCPool(void);
  9. ~CODBCPool(void);
  10. // 获取实例指针;
  11. static CODBCPool* GetInstance();
  12. // 初始化所有连接;
  13. 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);
  14. // 关闭所有连接;
  15. void ReleasePool(){DestroyAllDBConnections();}
  16. // 设置数据库信息;
  17. void SetDBConnectionInfo(IN LPCTSTR lpDBSource, IN CONST DWORD &dwDBPort, IN LPCTSTR lpDBAccount, IN LPCTSTR lpPassWord, IN LPCTSTR lpDBName);
  18. // 初始化所有连接;
  19. INT IntiAllConnections();
  20. // 断开所有连接;
  21. void DestroyAllDBConnections();
  22. // 获取一个空闲连接;
  23. CODBCConnect* GetAConnection( IN CONST DWORD &dwTimeOut = 1000 );
  24. // 交还连接给空闲队列;
  25. INT RestoreAConnection(IN CODBCConnect *pDBConn);
  26. private:
  27. volatile LONG m_nRef;
  28. volatile LONG m_nObjRef;
  29. // 创建一个连接;
  30. INT InitAConnection();
  31. // 关闭一个连接;
  32. void CloseAConnection(CODBCConnect* pDBEngine);
  33. // 停止工作者线程;
  34. void StopWorkThread();
  35. // 判断是否需要停止工作;
  36. BOOL IsNeedStop();
  37. // 判断是否需要连接;
  38. BOOL IsNeedConnection();
  39. // 将守卫类友元化;
  40. friend class ODBCConnGuard;
  41. // 单例实例;
  42. static CODBCPool* m_pInstance;
  43. // 空闲数据库连接池;
  44. ODBCConnectList m_listIdleConnections;
  45. // 在使用的数据库连接池;
  46. ODBCConnectList m_listBusyConnections;
  47. // 保护连接池的临界区
  48. CRITICAL_SECTION m_csIdleConnList;
  49. CRITICAL_SECTION m_csBusyConnList;
  50. // 可用的指标:最大、最小;
  51. INT m_nMaxCount;
  52. INT m_nMinCount;
  53. // 引入的指针变量;
  54. LPCTSTR m_lpDBSource;
  55. DWORD m_dwDBPort;
  56. LPCTSTR m_lpDBAccount;
  57. LPCTSTR m_lpPassWord;
  58. LPCTSTR m_lpDBName;
  59. // 维护线程;
  60. HANDLE m_hWorkThread;
  61. HANDLE m_hHaveData;
  62. BOOL m_bNeedExit; // 退出数据池;
  63. BOOL m_bNeedStop;
  64. BOOL m_bNeedConnection;
  65. static DWORD WINAPI WorkThread( IN LPVOID lpInput );
  66. public:
  67. BOOL GetSelectby(IN LPCTSTR lpTblName, IN LPCTSTR lpColumn, IN LPCTSTR lpFilters, OUT LPTSTR lpResult, IN CONST INT& nBufLen, IN CONST DWORD &dwTimeOut = 3000 );
  68. //////////////////////////////////////////////////////////////////////////
  69. // 数据库接口;
  70. BOOL ExecuteSQL( IN LPCTSTR lpExcuteSQL, IN CONST DWORD &dwTimeOut = 30000 );
  71. INT GetTblRecordCount( IN LPCTSTR lpTblName, IN LPCTSTR lpFilters, IN CONST DWORD &dwTimeOut = 30000 );
  72. //////////////////////////////////////////////////////////////////////////
  73. // 公司域名接收后台;
  74. BOOL GetunauthInfo( IN LPCTSTR lpSQLFilters, OUT LPTSTR lpUnauthInfo, IN CONST DWORD &dwTimeOut = 30000 );
  75. BOOL GetunauthInfo( IN LPCTSTR lpSQLFilters, OUT TString &strUnauthInfo, IN CONST DWORD &dwTimeOut = 30000 );
  76. INT GetClientEnterpriseName( IN LPCTSTR lpSQLFilters, OUT LPVOID lpClientIPInfo, IN CONST DWORD &dwTimeOut = 30000 );
  77. INT GetEnterprisInfo(IN LPCTSTR lpEnterpriseName, OUT CArray<CStringArray, CStringArray>& AryEnterpriseInfo, IN CONST DWORD &dwTimeOut = 30000);
  78. BOOL GetDBCFileInfo(IN LPCTSTR lpSQL, OUT TString& strResult, IN CONST DWORD &dwTimeOut = 3000);
  79. BOOL GetVersionInfo( OUT LPVOID lpVerInfo, IN CONST DWORD &dwTimeOut = 30000 );
  80. BOOL GetNetShareInfo( OUT LPVOID lpNetShareInfo, IN CONST DWORD &dwTimeOut = 30000 );
  81. BOOL GetOrderInfo( OUT LPVOID lpOrderInfo, IN CONST DWORD &dwTimeOut = 30000 );
  82. BOOL GetTookOrderInfo( OUT LPVOID lpTookOrderInfo, IN CONST DWORD &dwTimeOut = 3000 );
  83. };
  84. // ODBC守卫垫片类;
  85. class ODBCConnGuard
  86. {
  87. CODBCConnect *m_pODBCConn;
  88. public:
  89. ODBCConnGuard( CODBCConnect *&pDBConn, CONST DWORD &dwTimeOut = 30000 )
  90. {
  91. pDBConn = CODBCPool::GetInstance()->GetAConnection( dwTimeOut );
  92. m_pODBCConn = pDBConn;
  93. }
  94. virtual ~ODBCConnGuard()
  95. {
  96. CODBCPool::GetInstance()->RestoreAConnection(m_pODBCConn);
  97. }
  98. };
  99. #endif