⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectpool.c

📁 封装的c++访问mysql的连接池代码 支持windows和linux下的mysql
💻 C
字号:
//连接池的实现(ConnectPool, C++版) 


#include "public.h"
#include "ConnectPool.h"



//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//构造函数
ConnectPool::ConnectPool() 
{ 
	m_PoolSize = 0;
	m_BusyNum = 0;
	m_PeakNum = 0;
	m_Status = 0;

}
//析构函数
ConnectPool::~ConnectPool() 
{
	ClosePool();
}

//关闭连接池中的连接
int ConnectPool::ClosePool()
{
	MYSQL *pMysql = NULL;
	while(!m_ConnectQ.Empty())
	{
		m_ConnectQ.Delete(pMysql);
		mysql_close(pMysql) ;
	}

	return 1;
}

//重新连接
int	ConnectPool::ResetConnet(MYSQL* pMysql)
{
	mysql_close(pMysql);

	//------------------初始化-----------------------
	pMysql = mysql_init((MYSQL*)0);
	if(!pMysql)
	{
		return  -1;
	}

	//连接服务器
	//MYSQL_HOST,MYSQL_USER,MYSQL_PWS,MYSQL_PORT,MYSQL_DB
	if(!mysql_real_connect(pMysql,MYSQL_HOST,MYSQL_USER,MYSQL_PWS,NULL,MYSQL_PORT,NULL,0))
	{
		return -1;
	}

	//选择数据库
	if(mysql_select_db(pMysql,MYSQL_DB)<0) 
	{
		printf( "Can't select the %s database !\n",MYSQL_DB);
		mysql_close(pMysql) ;

		return -1;
	}

	return 1;
}

int ConnectPool::InitPool(int nSize,char *host,char *user,char *pws,int port,char *db)
{
	
	m_PoolSize = nSize;
	m_ConnectQ.Init(m_PoolSize);

	MYSQL *pMysql = NULL;
	for(int i=0; i<nSize; i++)
	{
		//------------------初始化-----------------------
		pMysql = mysql_init((MYSQL*)0);
		if(!pMysql)
		{
			m_Status = -1;
			printf("\r\n mysql init fail");
			return m_Status;
		}

		//连接服务器
		//if(!mysql_real_connect(&mysql,p->host,p->user,p->password,p->db,p->port,p->unix_socket,p->client_flag))
		if(!mysql_real_connect(pMysql,host,user,pws,NULL,port,NULL,0))
		{
			m_Status = -1;
			printf("\r\n mysql conncet fail");
			return m_Status;
			
		}

		//选择数据库
		if(mysql_select_db(pMysql,db)<0) 
		{
			printf( "Can't select the %s database !\n",db);
			mysql_close(pMysql) ;
			printf("\r\n mysql select db fail");
			m_Status = -1;
			return m_Status;
		}

		m_ConnectQ.Insert(pMysql);
		printf("\r\n inti mysql succeed \r\n");

	}

	m_Status = 1;
	return m_Status;

}

MYSQL* ConnectPool::GetConnet()
{
	MYSQL *pMysql = NULL;

	int nWaitNum = 0;
	while(1)
	{
		//存在空闲的连接
		if(!m_ConnectQ.Empty())
		{
			//连接出池
			m_BusyNum++;
			if(m_PeakNum<m_BusyNum)
				m_PeakNum = m_BusyNum;
			m_ConnectQ.Delete(pMysql);

			break;
		}
		//等待3秒没空闲连接就重新建立连接
		else
		{
			if(nWaitNum>3)
			{
				ClosePool();
				InitPool(CONNECTPOOL_SIZE,MYSQL_HOST,MYSQL_USER,MYSQL_PWS,MYSQL_PORT,MYSQL_DB);
			}
			else if(nWaitNum>4)
			{
				break;
			}
			
			nWaitNum++;
		}
	}


	return pMysql;
}

void ConnectPool::PutConnet(MYSQL* pMysql)
{
	m_BusyNum--;
	m_ConnectQ.Insert(pMysql);
}

//执行 SQL 语句
//支持多线程,表加锁:LOCK TABLES TABLENAME WRITE
int ConnectPool::ExeSql(char *pTableName,char *pSql)
{
	//得到连接
	MYSQL *pMysql = GetConnet();
	if(!pMysql)
	{
		return 0;
	}

	//表加锁
	char str[50];
	sprintf(str,"LOCK TABLES %s WRITE",pTableName);
    if(mysql_query(pMysql,str))
	{
		//连接入池
		PutConnet(pMysql);
		return -1;
	}

	//执行
	if(mysql_query(pMysql,pSql))
	{
		int id = mysql_insert_id(pMysql);

		//连接入池
		PutConnet(pMysql);

		//表解锁
		sprintf(str,"UNLOCK TABLES");
		if(mysql_query(pMysql,str))
		{
			return -1;
		}

		return 0;
	}
	
	//表解锁
	sprintf(str,"UNLOCK TABLES");
	if(mysql_query(pMysql,str))
	{
		//连接入池
		PutConnet(pMysql);
		return -1;
	}

	//连接入池
	PutConnet(pMysql);

	return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -