MysqlConnPool.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #ifndef YASERVER_MYSQLCONNPOOL_H_
  2. #define YASERVER_MYSQLCONNPOOL_H_
  3. #include <mysql.h>
  4. #include <errmsg.h>
  5. #include <list>
  6. #ifndef MAXCRITICALSECTIONSPINCOUNT
  7. #define MAXCRITICALSECTIONSPINCOUNT 4000
  8. #endif // !MAXCRITICALSECTIONSPINCOUNT
  9. using namespace std;
  10. DWORD WINAPI thread_run( LPVOID pdata);
  11. DWORD WINAPI thread_runforConn( LPVOID pdata);
  12. class CMysqlConnPool;
  13. class CMysqlConn{
  14. public:
  15. CMysqlConn(const char *option, const char *host, const char *user,
  16. const char *password, const char* dbname, unsigned int port,
  17. const char *unix_socket, unsigned long clientflag, bool &state);
  18. virtual ~CMysqlConn();
  19. public:
  20. // 连接到数据库
  21. int SetOption();
  22. int Open();
  23. // 关闭数据库clei
  24. int Close();
  25. // 数据库是否已连接
  26. bool IsOpen() const;
  27. // query
  28. MYSQL_RES * Execute(const char * strSQL, int &err);
  29. void MultiExecute( const char * strSQL, int &err);
  30. int Get_MysqlEror();
  31. inline void setPool(CMysqlConnPool * p){m_pool = p;}
  32. inline CMysqlConnPool* getPool(){return m_pool;}
  33. private:
  34. MYSQL * m_pConn;
  35. bool m_bValid; // 已被初始化成功标识
  36. bool m_bOpen;
  37. bool m_needTryConnect;
  38. const char * m_option;
  39. const char * m_host;
  40. const char * m_user;
  41. const char * m_pwd;
  42. const char * m_db;
  43. unsigned int m_port;
  44. const char * m_unix_socket;
  45. unsigned long m_clientflag;
  46. CMysqlConnPool *m_pool;
  47. };
  48. typedef std::list<CMysqlConn*> DBConnectList;
  49. class CMysqlConnPool
  50. {
  51. public:
  52. // 获取实例指针
  53. //static CMysqlConnPool &Instanse(){
  54. // static CMysqlConnPool m_pInstanse;
  55. // return m_pInstanse;
  56. //};
  57. // 初始化所有连接
  58. int InitAllConn();
  59. // 关闭所有连接
  60. void CloseAllConn();
  61. // 获取一个空闲连接
  62. CMysqlConn* GetNewConn();
  63. // 交还连接给空闲队列
  64. int RestoreConn(CMysqlConn* pDBEngine);
  65. void Init(const char* szOption, const char *szHost, const char *szUser, const char *szPwd, const char *szDbName,
  66. unsigned int nPort, const char * szUnixSocket, unsigned long nClientFlag);
  67. void IsNeedConn(){m_needConnect = true;}
  68. bool m_needConnect;
  69. CMysqlConnPool();
  70. virtual ~CMysqlConnPool();
  71. // 创建一个连接
  72. int InitNewConn();
  73. // 关闭一个连接
  74. void CloseConn(CMysqlConn* pDBEngine);
  75. // 停止工作线程
  76. void StopThread();
  77. // 判断是否需要停止
  78. BOOL IsNeedStop();
  79. BOOL IsNeedConnection();
  80. HANDLE m_hHaveData; // 信号
  81. private:
  82. // 将守卫类作为连接池类的友元类
  83. friend class CDBConnGuard;
  84. // 空闲数据库连接队列
  85. DBConnectList m_listIdleConnection;
  86. // 在使用的数据库连接
  87. DBConnectList m_listBusyConnection;
  88. // 队列保护的临界区
  89. CRITICAL_SECTION m_csIdleConnList;
  90. CRITICAL_SECTION m_csBusyConnList;
  91. // 可用连接总数的三个指标:最大、最小
  92. unsigned int m_nMaxCount;
  93. unsigned int m_nMinCount;
  94. // 数据库信息
  95. std::string m_strHost;
  96. std::string m_strUser;
  97. std::string m_strPwd;
  98. std::string m_strUnixSocket;
  99. std::string m_strOption;
  100. std::string m_strDbName;
  101. //const char * m_strHost;
  102. //const char * m_strUser;
  103. //const char * m_strPwd;
  104. //const char * m_strUnixSocket;
  105. //const char * m_strOption;
  106. //const char * m_strDbName;
  107. unsigned int m_nPort;
  108. unsigned long m_nClientFlag;
  109. // 维护线程
  110. HANDLE m_hMaintanceThread; // 线程句柄
  111. HANDLE m_hMaintanceThreadForConn; // 线程句柄
  112. BOOL m_bNeedStop; // 管理线程起停的标志位
  113. BOOL m_bNeedConnection; // 需要创建连接的标志
  114. };
  115. class CDBConnGuard{
  116. public:
  117. //CDBConnGuard(CMysqlConn* &pDbConn){
  118. // pDbConn = CMysqlConnPool::Instanse().GetNewConn();
  119. // //判断连接的有效性
  120. // m_pDbConn = pDbConn;
  121. //}
  122. //virtual ~CDBConnGuard()
  123. //{
  124. // CMysqlConnPool::Instanse().RestoreConn(m_pDbConn);
  125. //}
  126. CDBConnGuard(CMysqlConn* &pDbConn, CMysqlConnPool* pConnPool)
  127. :m_isVaild(false)
  128. {
  129. m_pDbConnPool = pConnPool;
  130. pDbConn = m_pDbConnPool->GetNewConn();
  131. m_pDbConn = pDbConn;
  132. }
  133. virtual ~ CDBConnGuard(){
  134. if (!m_isVaild)
  135. {
  136. m_pDbConnPool->RestoreConn(m_pDbConn);
  137. }
  138. m_pDbConnPool = NULL;
  139. }
  140. void isVaild(){m_isVaild = true;}
  141. private:
  142. CMysqlConn * m_pDbConn;
  143. bool m_isVaild;
  144. CMysqlConnPool * m_pDbConnPool;
  145. };
  146. //#define GetConnPool() CMysqlConnPool::Instanse()
  147. #endif //YASERVER_MYSQLCONNPOOL_H_