123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #ifndef YASERVER_MYSQLCONNPOOL_H_
- #define YASERVER_MYSQLCONNPOOL_H_
- #include <mysql.h>
- #include <errmsg.h>
- #include <list>
- #ifndef MAXCRITICALSECTIONSPINCOUNT
- #define MAXCRITICALSECTIONSPINCOUNT 4000
- #endif // !MAXCRITICALSECTIONSPINCOUNT
- using namespace std;
- DWORD WINAPI thread_run( LPVOID pdata);
- DWORD WINAPI thread_runforConn( LPVOID pdata);
- class CMysqlConnPool;
- class CMysqlConn{
- public:
- CMysqlConn(const char *option, const char *host, const char *user,
- const char *password, const char* dbname, unsigned int port,
- const char *unix_socket, unsigned long clientflag, bool &state);
- virtual ~CMysqlConn();
- public:
- // 连接到数据库
- int SetOption();
- int Open();
- // 关闭数据库clei
- int Close();
- // 数据库是否已连接
- bool IsOpen() const;
- // query
- MYSQL_RES * Execute(const char * strSQL, int &err);
- void MultiExecute( const char * strSQL, int &err);
- int Get_MysqlEror();
- inline void setPool(CMysqlConnPool * p){m_pool = p;}
- inline CMysqlConnPool* getPool(){return m_pool;}
- private:
- MYSQL * m_pConn;
- bool m_bValid; // 已被初始化成功标识
- bool m_bOpen;
- bool m_needTryConnect;
- const char * m_option;
- const char * m_host;
- const char * m_user;
- const char * m_pwd;
- const char * m_db;
- unsigned int m_port;
- const char * m_unix_socket;
- unsigned long m_clientflag;
- CMysqlConnPool *m_pool;
- };
- typedef std::list<CMysqlConn*> DBConnectList;
- class CMysqlConnPool
- {
- public:
- // 获取实例指针
- //static CMysqlConnPool &Instanse(){
- // static CMysqlConnPool m_pInstanse;
- // return m_pInstanse;
- //};
- // 初始化所有连接
- int InitAllConn();
- // 关闭所有连接
- void CloseAllConn();
- // 获取一个空闲连接
- CMysqlConn* GetNewConn();
- // 交还连接给空闲队列
- int RestoreConn(CMysqlConn* pDBEngine);
- void Init(const char* szOption, const char *szHost, const char *szUser, const char *szPwd, const char *szDbName,
- unsigned int nPort, const char * szUnixSocket, unsigned long nClientFlag);
- void IsNeedConn(){m_needConnect = true;}
- bool m_needConnect;
- CMysqlConnPool();
- virtual ~CMysqlConnPool();
- // 创建一个连接
- int InitNewConn();
- // 关闭一个连接
- void CloseConn(CMysqlConn* pDBEngine);
- // 停止工作线程
- void StopThread();
- // 判断是否需要停止
- BOOL IsNeedStop();
- BOOL IsNeedConnection();
- HANDLE m_hHaveData; // 信号
- private:
- // 将守卫类作为连接池类的友元类
- friend class CDBConnGuard;
- // 空闲数据库连接队列
- DBConnectList m_listIdleConnection;
- // 在使用的数据库连接
- DBConnectList m_listBusyConnection;
- // 队列保护的临界区
- CRITICAL_SECTION m_csIdleConnList;
- CRITICAL_SECTION m_csBusyConnList;
- // 可用连接总数的三个指标:最大、最小
- unsigned int m_nMaxCount;
- unsigned int m_nMinCount;
- // 数据库信息
- std::string m_strHost;
- std::string m_strUser;
- std::string m_strPwd;
- std::string m_strUnixSocket;
- std::string m_strOption;
- std::string m_strDbName;
- //const char * m_strHost;
- //const char * m_strUser;
- //const char * m_strPwd;
- //const char * m_strUnixSocket;
- //const char * m_strOption;
- //const char * m_strDbName;
- unsigned int m_nPort;
- unsigned long m_nClientFlag;
- // 维护线程
- HANDLE m_hMaintanceThread; // 线程句柄
- HANDLE m_hMaintanceThreadForConn; // 线程句柄
- BOOL m_bNeedStop; // 管理线程起停的标志位
- BOOL m_bNeedConnection; // 需要创建连接的标志
-
- };
- class CDBConnGuard{
- public:
- //CDBConnGuard(CMysqlConn* &pDbConn){
- // pDbConn = CMysqlConnPool::Instanse().GetNewConn();
- // //判断连接的有效性
- // m_pDbConn = pDbConn;
- //}
- //virtual ~CDBConnGuard()
- //{
- // CMysqlConnPool::Instanse().RestoreConn(m_pDbConn);
- //}
- CDBConnGuard(CMysqlConn* &pDbConn, CMysqlConnPool* pConnPool)
- :m_isVaild(false)
- {
- m_pDbConnPool = pConnPool;
- pDbConn = m_pDbConnPool->GetNewConn();
- m_pDbConn = pDbConn;
- }
- virtual ~ CDBConnGuard(){
- if (!m_isVaild)
- {
- m_pDbConnPool->RestoreConn(m_pDbConn);
- }
- m_pDbConnPool = NULL;
- }
- void isVaild(){m_isVaild = true;}
- private:
- CMysqlConn * m_pDbConn;
- bool m_isVaild;
- CMysqlConnPool * m_pDbConnPool;
- };
- //#define GetConnPool() CMysqlConnPool::Instanse()
- #endif //YASERVER_MYSQLCONNPOOL_H_
|