WxAdoPool.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #ifndef __ODBC_POOL__
  2. #define __ODBC_POOL__
  3. #pragma once
  4. #include "SafeList.h"
  5. //#include <afxdb.h>
  6. typedef SafeList<AdoObj*> AdoConnList;
  7. class CWxAdoPool
  8. {
  9. ThreadSection m_critSection;
  10. // 空闲数据库连接池;
  11. AdoConnList m_listIdleConnections;
  12. // 在使用的数据库连接池;
  13. AdoConnList m_listBusyConnections;
  14. // 可用的指标:最大、最小;
  15. INT m_nMaxCount;
  16. INT m_nMinCount;
  17. // 引入的指针变量;
  18. DWORD m_dwDBPort;
  19. CString m_strDBType;
  20. CString m_strDBSource;
  21. CString m_strDBAccount;
  22. CString m_strPassWord;
  23. CString m_strDBName;
  24. BOOL m_bNeedExit; // 退出数据池;
  25. BOOL m_bNeedStop;
  26. BOOL m_bNeedConnection;
  27. public:
  28. CWxAdoPool(void) ;
  29. ~CWxAdoPool(void);
  30. // 获取实例指针;
  31. static CWxAdoPool* GetInstance()
  32. {
  33. static CWxAdoPool* pInstance = NULL;
  34. if ( pInstance == NULL )
  35. pInstance = new CWxAdoPool;
  36. return pInstance;
  37. }
  38. // 初始化所有连接;
  39. DWORD InitializePool(
  40. IN LPCTSTR lpDBType,
  41. IN LPCTSTR lpDBSource,
  42. IN CONST DWORD &dwDBPort,
  43. IN LPCTSTR lpDBAccount,
  44. IN LPCTSTR lpPassWord,
  45. IN LPCTSTR lpDBName,
  46. IN CONST INT &nMinConn = 1,
  47. IN CONST INT &nMaxConn = 5
  48. );
  49. // 关闭所有连接;
  50. void ReleasePool();
  51. // 设置数据库信息;
  52. void SetDBConnectionInfo(IN LPCTSTR lpDBSource, IN CONST DWORD &dwDBPort, IN LPCTSTR lpDBAccount, IN LPCTSTR lpPassWord, IN LPCTSTR lpDBName);
  53. // 初始化所有连接;
  54. INT IntiAllConnections();
  55. // 断开所有连接;
  56. void DestroyAllDBConnections();
  57. // 获取一个空闲连接;
  58. pAdoObj GetAConnection( IN CONST DWORD &dwTimeOut = 1000 );
  59. // 交还连接给空闲队列;
  60. void RestoreAConnection(IN pAdoObj pDBEngine);
  61. // 关闭一个连接;
  62. void CloseAConnection(pAdoObj &pDBEngine);
  63. // 关闭使用的连接;
  64. void CloseBusyConnection(IN pAdoObj &pDataBase);
  65. private:
  66. volatile LONG m_nRef;
  67. volatile LONG m_nObjRef;
  68. // 创建一个连接;
  69. pAdoObj InitAConnection();
  70. // 将守卫类友元化;
  71. friend class CAdoConnGuard;
  72. };
  73. // ODBC守卫垫片类;
  74. class CAdoConnGuard
  75. {
  76. pAdoObj m_pAdoConn;
  77. public:
  78. CAdoConnGuard(pAdoObj &pDBConn, CONST DWORD &dwTimeOut = 30000 ):m_pAdoConn(NULL)
  79. {
  80. pDBConn = CWxAdoPool::GetInstance()->GetAConnection( dwTimeOut );
  81. m_pAdoConn = pDBConn;
  82. }
  83. virtual ~CAdoConnGuard()
  84. {
  85. CWxAdoPool::GetInstance()->RestoreAConnection(m_pAdoConn);
  86. }
  87. };
  88. #endif // __SAFE_LIST__