MysqlConnPool.h 3.1 KB

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