123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- #include "StdAfx.h"
- #include "MysqlConnPool.h"
- #include "./system_basic_info/SystemAnalysis.h"
- CMysqlConnPool::CMysqlConnPool()
- {
- m_nMinCount = 3;
- m_nMaxCount = 30;
- m_bNeedStop = FALSE;
- m_bNeedConnection = FALSE;
- m_hMaintanceThread = INVALID_HANDLE_VALUE;
- m_hMaintanceThread = CreateEvent(NULL, TRUE, FALSE, _T("DataConnPool"));
-
- InitializeCriticalSectionAndSpinCount(&m_csIdleConnList, MAXCRITICALSECTIONSPINCOUNT);
- InitializeCriticalSectionAndSpinCount(&m_csBusyConnList, MAXCRITICALSECTIONSPINCOUNT);
- }
- CMysqlConnPool::~CMysqlConnPool()
- {
- m_hMaintanceThread = INVALID_HANDLE_VALUE;
- m_bNeedStop = TRUE;
- CloseHandle(m_hHaveData);
- CloseHandle(m_hMaintanceThread);
- CloseAllConn();
- DeleteCriticalSection(&m_csIdleConnList);
- DeleteCriticalSection(&m_csBusyConnList);
- }
- int CMysqlConnPool::InitAllConn()
- {
- CloseAllConn();
-
-
- int nCount = 0;
- for(unsigned int i = 0; i < m_nMinCount; i++)
- {
- nCount = InitNewConn();
- }
-
- if(INVALID_HANDLE_VALUE == m_hMaintanceThread)
- {
- m_hMaintanceThread = CreateThread(NULL, NULL, thread_run, (LPVOID)this, 0, NULL);
- }
- return nCount;
- }
- void CMysqlConnPool::CloseAllConn()
- {
-
- EnterCriticalSection(&m_csIdleConnList);
- DBConnectList::iterator itIdle = m_listIdleConnection.begin();
- for (; itIdle != m_listIdleConnection.end();)
- {
- if(NULL != (*itIdle)){
- (*itIdle)->Close();
- delete (*itIdle);
- }
- itIdle = m_listIdleConnection.erase(itIdle);
- }
- LeaveCriticalSection(&m_csIdleConnList);
-
-
- EnterCriticalSection(&m_csBusyConnList);
- DBConnectList::iterator itBusy = m_listBusyConnection.begin();
- for(; itBusy != m_listBusyConnection.end();)
- {
- if(NULL != (*itBusy))
- {
- (*itBusy)->Close();
- delete(*itBusy);
- }
-
- m_listBusyConnection.erase(itBusy++);
- }
- LeaveCriticalSection(&m_csBusyConnList);
- }
- CMysqlConn* CMysqlConnPool::GetNewConn()
- {
- CMysqlConn * pDBEngine = NULL;
-
- int nTimes = 0;
- while ((m_listIdleConnection.size() <= 0) && (nTimes < 5))
- {
- Sleep(1000);
- nTimes++;
- }
- if (5 == nTimes)
- {
-
-
- return pDBEngine;
- }
-
- EnterCriticalSection(&m_csIdleConnList);
- if (m_listIdleConnection.size() > 0)
- {
- pDBEngine = m_listIdleConnection.front();
- m_listIdleConnection.pop_front();
-
- EnterCriticalSection(&m_csBusyConnList);
- m_listBusyConnection.push_back(pDBEngine);
- LeaveCriticalSection(&m_csBusyConnList);
- }
- LeaveCriticalSection(&m_csIdleConnList);
- if (m_listIdleConnection.size() <= 1)
- {
-
- if ((m_listIdleConnection.size() + m_listBusyConnection.size()) < m_nMaxCount)
- {
-
- SetEvent(m_hHaveData);
- m_bNeedConnection = TRUE;
- }
- else
- {
-
-
- }
- }
- return pDBEngine;
- }
- int CMysqlConnPool::RestoreConn( CMysqlConn* pDBEngine )
- {
- if (NULL != pDBEngine)
- {
-
- EnterCriticalSection(&m_csBusyConnList);
- m_listBusyConnection.remove(pDBEngine);
- LeaveCriticalSection(&m_csBusyConnList);
-
- EnterCriticalSection(&m_csIdleConnList);
- m_listIdleConnection.push_back(pDBEngine);
- LeaveCriticalSection(&m_csIdleConnList);
- }
- EnterCriticalSection(&m_csIdleConnList);
- int nCount = m_listIdleConnection.size();
- LeaveCriticalSection(&m_csIdleConnList);
- return nCount;
- }
- void CMysqlConnPool::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 )
- {
- m_strOption = szOption;
- m_strHost = szHost;
- m_strUser = szUser;
- m_strPwd = szPwd;
- m_strDbName = szDbName;
- m_nPort = nPort;
- if(szUnixSocket != NULL){
- m_strUnixSocket = szUnixSocket;
- }
- m_nClientFlag = nClientFlag;
- }
- int CMysqlConnPool::InitNewConn()
- {
- bool bSuccess = false;
- CMysqlConn * pDBEngine = new CMysqlConn(m_strOption.c_str(), m_strHost.c_str(), m_strUser.c_str(), m_strPwd.c_str(), m_strDbName.c_str(),
- m_nPort, m_strUnixSocket.c_str(), m_nClientFlag, bSuccess);
- if (bSuccess)
- {
- m_bNeedConnection = FALSE;
- return RestoreConn(pDBEngine);
- }
- else
- {
- delete pDBEngine;
- return m_listIdleConnection.size();
- }
- }
- void CMysqlConnPool::CloseConn( CMysqlConn* pDBEngine )
- {
- pDBEngine->Close();
-
- EnterCriticalSection(&m_csIdleConnList);
- m_listIdleConnection.remove(pDBEngine);
- LeaveCriticalSection(&m_csIdleConnList);
- }
- void CMysqlConnPool::StopThread()
- {
- m_bNeedStop = TRUE;
-
- SetEvent(m_hHaveData);
-
- WaitForSingleObject(m_hMaintanceThread, INFINITE);
- CloseHandle(m_hMaintanceThread);
- }
- BOOL CMysqlConnPool::IsNeedStop()
- {
- return m_bNeedStop;
- }
- BOOL CMysqlConnPool::IsNeedConnection()
- {
- return m_bNeedConnection;
- }
- DWORD WINAPI CMysqlConnPool::thread_run( LPVOID pdata )
- {
- CMysqlConnPool * pConPool = (CMysqlConnPool *) pdata;
- while (!pConPool->IsNeedStop())
- {
-
- ResetEvent(pConPool->m_hHaveData);
- WaitForSingleObject(pConPool->m_hHaveData, INFINITE);
- if (pConPool->IsNeedConnection())
- {
-
- pConPool->InitNewConn();
- }
- }
-
- return 0;
- }
- CMysqlConn::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 )
- {
- m_option = option;
- m_host = host;
- m_user = user;
- m_pwd = password;
- m_db = dbname;
- m_port = port;
- m_unix_socket = unix_socket;
- m_clientflag = clientflag;
- m_bOpen = false;
- m_pConn = mysql_init(NULL);
- if( 0 == SetOption())
- {
- if(0 == Open())
- {
- state = true;
- }else{
- state = false;
- }
- }else{
- state = false;
- }
- }
- CMysqlConn::~CMysqlConn()
- {
- Close();
- }
- int CMysqlConn::SetOption()
- {
- if(mysql_options(m_pConn, MYSQL_SET_CHARSET_NAME, m_option))
- return 1;
- return 0;
- }
- int CMysqlConn::Open()
- {
- HRESULT hr = ::CoInitialize(NULL);
- if(FAILED(hr))
- return -1;
-
- m_clientflag |= CLIENT_MULTI_STATEMENTS;
- if(!mysql_real_connect(m_pConn, m_host, m_user, m_pwd, m_db, m_port, m_unix_socket, m_clientflag))
- return 1;
- m_bOpen = true;
- return 0;
- }
- int CMysqlConn::Close()
- {
- if (m_bOpen)
- {
- mysql_close(m_pConn);
- ::CoUninitialize();
- m_bOpen = false;
- }
- return 0;
- }
- bool CMysqlConn::IsOpen() const
- {
- return m_bOpen;
- }
- MYSQL_RES * CMysqlConn::Execute( const char * strSQL, int &err)
- {
- if(strSQL == "") return NULL;
- MYSQL_RES * pRes;
- int _error = mysql_query(m_pConn, strSQL);
- err = _error;
- if(_error){
- return NULL;
- }
- pRes = mysql_use_result(m_pConn);
- return pRes;
- }
- void CMysqlConn::MultiExecute( const char * strSQL, int &err)
- {
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_89);
- if(strSQL == "")
- err = 0;
- MYSQL_RES * pRes;
- err = mysql_query(m_pConn, strSQL);
-
-
- do
- { LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_90);
- pRes = mysql_use_result(m_pConn);
- mysql_free_result(pRes);
- } while (!mysql_next_result(m_pConn));
- }
|