MysqlConnPool.h 3.0 KB

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