123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #ifndef YASERVER_MYSQLCONNPOOL_H_
- #define YASERVER_MYSQLCONNPOOL_H_
- #include <mysql.h>
- #include <list>
- #ifndef MAXCRITICALSECTIONSPINCOUNT
- #define MAXCRITICALSECTIONSPINCOUNT 4000
- #endif
- using namespace std;
- 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();
-
- int Close();
-
- bool IsOpen() const;
-
- MYSQL_RES * Execute(const char * strSQL, int &err);
- void MultiExecute( const char * strSQL, int &err);
- private:
- MYSQL * m_pConn;
- bool m_bValid;
- bool m_bOpen;
- 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;
- };
- 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);
- private:
-
- CMysqlConnPool();
- virtual ~CMysqlConnPool();
-
- int InitNewConn();
-
- void CloseConn(CMysqlConn* pDBEngine);
-
- void StopThread();
-
- BOOL IsNeedStop();
- BOOL IsNeedConnection();
-
- 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;
-
-
-
-
-
-
- unsigned int m_nPort;
- unsigned long m_nClientFlag;
-
- HANDLE m_hMaintanceThread;
- HANDLE m_hHaveData;
- BOOL m_bNeedStop;
- BOOL m_bNeedConnection;
- static DWORD WINAPI thread_run( LPVOID pdata);
- };
- class CDBConnGuard{
- public:
- CDBConnGuard(CMysqlConn* &pDbConn){
- pDbConn = CMysqlConnPool::Instanse().GetNewConn();
-
- m_pDbConn = pDbConn;
- }
- virtual ~CDBConnGuard()
- {
- CMysqlConnPool::Instanse().RestoreConn(m_pDbConn);
- }
- private:
- CMysqlConn * m_pDbConn;
- };
- #define GetConnPool() CMysqlConnPool::Instanse()
- #endif
|