DBConnPool.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // DBConnPool.h: interface for the DBConnPool class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #if !defined(AFX_DBCONNPOOL_H__42089E9A_FD85_4DD4_A973_64A4980332A5__INCLUDED_)
  5. #define AFX_DBCONNPOOL_H__42089E9A_FD85_4DD4_A973_64A4980332A5__INCLUDED_
  6. #define SQL_BUFFER_LEN 1024
  7. #import "C:\Program Files\Common Files\System\ado\msado15.dll" rename("EOF","ADOEOF")
  8. #include <list>
  9. #include <windows.h>
  10. #include <string>
  11. #include <vector>
  12. #include "DBDefine.h"
  13. using namespace ADODB;
  14. using namespace std;
  15. class RFIDAPI DBConnect
  16. {
  17. public:
  18. DBConnect(LPCSTR strDstAddress,
  19. LPCSTR strUsername,
  20. LPCSTR strPassword,
  21. LPCSTR strDBName,
  22. BOOL &state);
  23. ~DBConnect();
  24. public:
  25. // 连接到数据库
  26. int Open(LPCTSTR strDstAddress, LPCTSTR strUsername, LPCTSTR strPassword, LPCTSTR strDBName);
  27. // 关闭数据库
  28. int Close();
  29. // 数据库是否已连接
  30. BOOL IsOpen() const;
  31. public:
  32. private:
  33. _ConnectionPtr _connection_ptr; //ADO的数据库连接智能指针
  34. bool _isAdoValid; //ADO环境是否已经初化成功标志量
  35. bool m_bDBOpen;
  36. LPCSTR _strDstAddress; //数据源地址或服务名
  37. LPCSTR _strUsername; //数据库用户名
  38. LPCSTR _strPassword; //数据库密码
  39. LPCSTR _strDBName; //数据库名称
  40. void VarientToString(_variant_t var, string& str);
  41. //对外公共接口
  42. public:
  43. int GetSubmitInfo(vector<SOAP_SUBMIT_SMS> &vecsoapSms);
  44. int InsertHistory(int id);
  45. };
  46. typedef std::list<DBConnect*> DBConnectList;
  47. class DBConnPool
  48. {
  49. public:
  50. DBConnPool();
  51. virtual ~DBConnPool();
  52. // 获取实例指针
  53. static DBConnPool * Instanse();
  54. // 初始化所有连接
  55. int InitializeAllDBConnections();
  56. // 关闭所有连接
  57. void DestroyAllDBConnections();
  58. // 获取一个空闲连接
  59. DBConnect* GetAConnection();
  60. // 交还连接给空闲队列
  61. int RestoreAConnection(DBConnect* pDBEngine);
  62. void SetDBInfo(LPCSTR strDstAddress, LPCSTR strUsername, LPCSTR strPassword, LPCSTR strDBName,int minConn,int maxConn);
  63. private:
  64. // 创建一个连接
  65. int InitializeAConnection();
  66. // 关闭一个连接
  67. void CloseAConnection(DBConnect* pDBEngine);
  68. // 停止工作线程
  69. void StopThread();
  70. // 判断是否需要停止
  71. BOOL IsNeedStop();
  72. BOOL IsNeedConnection();
  73. // 将守卫类作为连接池类的友元类
  74. friend class ConnGuard;
  75. // 唯一实例
  76. static DBConnPool *m_pInstanse;
  77. // 空闲数据库连接队列
  78. DBConnectList m_listIdleConnection;
  79. // 在使用的数据库连接
  80. DBConnectList m_listBusyConnection;
  81. // 队列保护的临界区
  82. CRITICAL_SECTION m_csIdleConnList;
  83. CRITICAL_SECTION m_csBusyConnList;
  84. // 可用连接总数的三个指标:最大、最小
  85. int m_nMaxCount;
  86. int m_nMinCount;
  87. // // 数据库信息
  88. LPCSTR _strDstAddress; //数据源地址或服务名
  89. LPCSTR _strUsername; //数据库用户名
  90. LPCSTR _strPassword; //数据库密码
  91. LPCSTR _strDBName; //数据库名称
  92. // 维护线程
  93. HANDLE m_hMaintanceThread; // 线程句柄
  94. HANDLE m_hHaveData; // 信号
  95. BOOL m_bNeedStop; // 管理线程起停的标志位
  96. BOOL m_bNeedConnection; // 需要创建连接的标志
  97. static DWORD WINAPI thread_run( LPVOID pdata);
  98. };
  99. // 守卫类,利用构造和析构函数保证连接取出和归还必须成对,防止资源泄露
  100. class DBConnGuard
  101. {
  102. public:
  103. DBConnGuard(DBConnect*& DBConn)
  104. {
  105. DBConn = DBConnPool::Instanse()->GetAConnection();
  106. m_pDBConn = DBConn;
  107. }
  108. virtual ~DBConnGuard()
  109. {
  110. DBConnPool::Instanse()->RestoreAConnection(m_pDBConn);
  111. }
  112. private:
  113. DBConnect *m_pDBConn;
  114. };
  115. RFIDAPI void InitDBIterface(LPCSTR strDstAddress, LPCSTR strUsername, LPCSTR strPassword, LPCSTR strDBName,int minConn,int maxConn);
  116. RFIDAPI DBConnect * GetAConnect();
  117. #endif // !defined(AFX_DBCONNPOOL_H__42089E9A_FD85_4DD4_A973_64A4980332A5__INCLUDED_)