SoFunction
Updated on 2025-03-09

MySQL database query optimization mysql efficiency page 3/3


//Define the handle queue
typedef std::list<MYSQL *> CONNECTION_HANDLE_LIST; 
typedef std::list<MYSQL *>::iterator CONNECTION_HANDLE_LIST_IT; 

//The parameter structure of the connection database
class CDBParameter              

public: 
char *host;
char *user;                                                                                                                           �
char *password;                                                                                                                                                                                                                                                           �
char *database;                                                                                                                           �
unsigned int port;
const char *unix_socket;       ///<Socket, generally NULL
unsigned int client_flag; ///<usually 0
}; 

//Create two queues
CONNECTION_HANDLE_LIST m_lsBusyList;                                                                                                                                                                                                                                                   �
CONNECTION_HANDLE_LIST m_lsIdleList;

//All connection handles are first connected to the database, added to the idle queue, and waiting for use.
bool CDBManager::Connect(char * host /* = "localhost" */, char * user /* = "chenmin" */, \ 
                                           char * password /* = "chenmin" */, char * database /* = "HostCache" */) 

       CDBParameter * lpDBParam = new CDBParameter(); 
       lpDBParam->host = host; 
       lpDBParam->user = user; 
       lpDBParam->password = password; 
       lpDBParam->database = database; 
       lpDBParam->port = 0; 
       lpDBParam->unix_socket = NULL; 
       lpDBParam->client_flag = 0; 
       try 
       { 
//connect
              for(int index = 0; index < CONNECTION_NUM; index++) 
              { 
MYSQL* pConnectHandle = mysql_init((MYSQL*) 0);      //Initialize the connection handle
                     if(!mysql_real_connect(pConnectHandle, lpDBParam->host, lpDBParam->user, lpDBParam->password,\ 
       lpDBParam->database,lpDBParam->port,lpDBParam->unix_socket,lpDBParam->client_fla)) 
                            return false; 
//Join to the idle queue
                     m_lsIdleList.push_back(pConnectHandle); 
              } 
       } 
       catch(...) 
       { 
              return false; 
       } 
       return true; 


//Extract an idle handle for use
MYSQL * CDBManager::GetIdleConnectHandle() 

       MYSQL * pConnectHandle = NULL; 
       m_ListMutex.acquire(); 
       if(m_lsIdleList.size()) 
       { 
              pConnectHandle = m_lsIdleList.front();        
              m_lsIdleList.pop_front(); 
              m_lsBusyList.push_back(pConnectHandle); 
       } 
else //Special case, the idle queue is empty, return is empty
       { 
              pConnectHandle = 0; 
       } 
       m_ListMutex.release(); 

       return pConnectHandle; 


//Release a used handle from the usage queue and insert it into the idle queue.
void CDBManager::SetIdleConnectHandle(MYSQL * connecthandle) 

       m_ListMutex.acquire(); 
       m_lsBusyList.remove(connecthandle); 
       m_lsIdleList.push_back(connecthandle); 
       m_ListMutex.release(); 

//Usage example, first get the idle handle, use this handle to do the real operation, and then insert it back to the idle queue.
bool CDBManager::DeleteHostCacheBySessionID(char * sessionid) 

       MYSQL * pConnectHandle = GetIdleConnectHandle(); 
       if(!pConnectHandle) 
              return 0; 
       bool bRet = DeleteHostCacheBySessionID(pConnectHandle, sessionid); 
       SetIdleConnectHandle(pConnectHandle); 
       return bRet; 

//Pass an idle handle and perform real deletion operation.
bool CDBManager::DeleteHostCacheBySessionID(MYSQL * connecthandle, char * sessionid) 

       char deleteSQL[SQL_LENGTH]; 
       memset(deleteSQL, 0, sizeof(deleteSQL)); 
       sprintf(deleteSQL,"delete from HostCache where SessionID = '%s'", sessionid); 
if(mysql_query(connectthandle,deleteSQL) != 0) //Delete
              return false; 
       return true;