#include "MysqlConnectPool.h"
//CMysqlConnectPool theMysqlConnectPool;
CMysqlConnectPool::CMysqlConnectPool(LPCTSTR lpHostName,
LPCTSTR lpUserName,
LPCTSTR lpPassword,
LPCTSTR lpDatabase)
{
if(lpHostName != NULL)
{
m_pHostName = new char[strlen(lpHostName) + 1];
memset(m_pHostName, 0, strlen(lpHostName) + 1);
strcpy(m_pHostName, lpHostName);
}
else
{
m_pHostName = NULL;
}
if(lpUserName != NULL)
{
m_pUserName = new char[strlen(lpUserName) + 1];
memset(m_pUserName, 0, strlen(lpUserName) + 1);
strcpy(m_pUserName,lpUserName);
}
else
{
m_pUserName = NULL;
}
if(lpPassword != NULL)
{
m_pPassword = new char[strlen(lpPassword) +1];
memset(m_pPassword, 0, strlen(lpPassword) +1);
strcpy(m_pPassword,lpPassword);
}
else
{
lpPassword == NULL;
}
if(lpDatabase != NULL)
{
m_pDatabase = new char[strlen(lpDatabase) +1];
memset(m_pDatabase, 0, strlen(lpDatabase) +1);
strcpy(m_pDatabase,lpDatabase);
}
else
{
m_pDatabase == NULL;
}
}
CMysqlConnectPool::~CMysqlConnectPool()
{
if(m_pHostName != NULL)
{
delete m_pHostName;
}
if(m_pUserName != NULL)
{
delete m_pUserName;
}
if(m_pPassword != NULL)
{
delete m_pPassword;
}
if(m_pDatabase != NULL)
{
delete m_pDatabase;
}
}
BOOL CMysqlConnectPool::InitConnectPool(UINT nInitConnectCount)
{
if(m_pHostName == NULL) return FALSE;
if(m_pUserName == NULL) return FALSE;
if(m_pPassword == NULL) return FALSE;
if(m_pDatabase == NULL) return FALSE;
for(int i = 0; i < nInitConnectCount; i++)
{
CSQLHandler *pHandler = new CSQLHandler;
pHandler->ConnectToDB(m_pHostName,m_pUserName,m_pPassword,m_pDatabase);
m_IdleListMutex.Lock();
m_IdleHandlerList.push_back(pHandler);
m_IdleListMutex.Unlock();
}
}
void CMysqlConnectPool::CloseConnectPool()
{
m_IdleListMutex.Lock();
for(int i = 0; i < m_IdleHandlerList.size(); i++)
{
CSQLHandler *m_pHandler=m_IdleHandlerList[i];
if(m_pHandler != NULL)
{
m_pHandler->CloseHandler();
// mysql_close(m_pHandler->m_pConnectHandler);
delete m_pHandler;
}
else
{
m_pHandler == NULL;
// syslog(LOG_USER|LOG_INFO,"MysqlconnectPool::closeConnectpool error!");
// return 0;
}
}
m_IdleHandlerList.clear();
m_IdleListMutex.Unlock();
m_BusyListMutex.Lock();
for(int i=0;i<m_BusyHandlerList.size();i++)
{
CSQLHandler *m_pHandler=m_BusyHandlerList[i];
if(m_pHandler != NULL)
{
m_pHandler->CloseHandler();
//mysql_close(m_pHandler->m_pConnectHandler);
delete m_pHandler;
}
else
{
m_pHandler = NULL;
}
}
m_BusyHandlerList.clear();
m_BusyListMutex.Unlock();
syslog(LOG_USER|LOG_INFO, "idllist size %s\n", m_IdleHandlerList.size());
syslog(LOG_USER|LOG_INFO, "busy size %s\n", m_BusyHandlerList.size());
}
CSQLHandler* CMysqlConnectPool::GetMysqlConnect()
{
CSQLHandler *m_pHandler = NULL;
m_IdleListMutex.Lock();
if(m_IdleHandlerList.size()<0)
{
syslog(LOG_USER|LOG_INFO, "new pool 10 ");
InitConnectPool(10);
}
m_pHandler = m_IdleHandlerList[0];
vector<CSQLHandler*>::iterator pos = m_IdleHandlerList.begin();
for(pos = m_IdleHandlerList.begin(); pos < m_IdleHandlerList.end(); pos++)
{
if(m_pHandler == *pos) break;
}
if(pos!=m_IdleHandlerList.end())
{
m_IdleHandlerList.erase(pos);
}
m_IdleListMutex.Unlock();
m_BusyListMutex.Lock();
m_BusyHandlerList.push_back(m_pHandler);
m_BusyListMutex.Unlock();
syslog(LOG_USER|LOG_INFO,"GET mysql connect success ");
return m_pHandler;
}
void CMysqlConnectPool::FreeMysqlConnect(CSQLHandler *m_pHandler)
{
m_IdleListMutex.Lock();
m_IdleHandlerList.push_back(m_pHandler);
m_IdleListMutex.Unlock();
m_BusyListMutex.Lock();
vector<CSQLHandler*>::iterator pos=m_BusyHandlerList.begin();
for(pos=m_BusyHandlerList.begin();pos!=m_BusyHandlerList.end();++pos)
{
if(m_pHandler==*pos) break;
}
m_BusyHandlerList.erase(pos);
m_BusyListMutex.Unlock();
m_IdleListMutex.Lock();
if(m_IdleHandlerList.size() > 50)
{
vector<CSQLHandler*>::iterator Idlpos=m_IdleHandlerList.begin();
for(Idlpos=m_IdleHandlerList.begin()+50;Idlpos < m_IdleHandlerList.end();Idlpos++)
{
CSQLHandler *m_pHandler = NULL;
m_pHandler = *Idlpos;
m_pHandler->CloseHandler();
//mysql_close(m_pHandler->m_pConnectHandler);
m_IdleHandlerList.erase(Idlpos);
}
}
m_IdleListMutex.Unlock();
}
int main()
{}