C++ 实现以简单的mysql连接池

实现以连接池,欢迎大家指正

my_Pool.h

/*************************************************************************
	> File Name: my_Pool.h
	> Author: LCG
	> Mail: 
	> Created Time: Fri Apr 29 11:21:56 2016
 ************************************************************************/
#ifndef _MY_POOL_
#define _MY_POOL_

#include<stdio.h>
#include<list>
#include<mysql.h>
#include<error.h>
#include<mysqld_error.h>
#include<string.h>
#include<iostream>
#include<string>

using namespace std;

class my_Pool
{
	public:
		my_Pool(string IP,int Port,string User,string PassWord,string DataBaseName,unsigned int MaxConn);			//
		MYSQL * GetConnection();						//get a mysql connection
		bool ReleaseConnection(MYSQL* conn);			//Release a Connection
		void DestoryPool();			//destory all the pool*/
		static my_Pool * GetInstance(string IP,int Port,string User,string PWD,string DataName,unsigned int MaxConn);
		int GetFreeConn();
	public:
		~my_Pool();
	private:
		unsigned int MaxConn;
		unsigned int UsedConn;
		unsigned int FreeConn;
	private:
		pthread_mutex_t lock;
		list<MYSQL *> connList;
		my_Pool * conn;
		MYSQL *Con;
		static my_Pool *connPool;
	private:
		string IP;
		string Port;
		string User;
		string PassWord;
		string DataName;
};

#endif

my_Poo.cpp

/*************************************************************************
	> File Name: my_Pool.cpp
	> Author: LCG
	> Mail: 
	> Created Time: Fri Apr 29 12:33:15 2016
 ************************************************************************/

#include<mysql.h>
#include<stdio.h>
#include<string>
#include<string.h>
#include<stdlib.h>
#include<list>
#include<pthread.h>
#include<iostream>

#include "my_Pool.h"

using namespace std;

my_Pool* my_Pool::connPool = NULL;

my_Pool::my_Pool(string IP,int Port,string User,string PassWord,string DBName,unsigned int MaxConn)
{
	this->IP = IP;
	this->Port = Port;
	this->User = User;
	this->PassWord = PassWord;

	pthread_mutex_lock(&lock);

	for(int i = 0; i < MaxConn; i++)
	{
		MYSQL * con = NULL;
		con = mysql_init(NULL);
		if(con == NULL)
		{
			cout<<"Error:"<<mysql_error(con);
			exit(1);
		}

		con = mysql_real_connect(con,IP.c_str(),User.c_str(),PassWord.c_str(),DBName.c_str(),0,NULL,0);
		if(con == NULL)
		{
			cout<<"Error: "<<mysql_error(con);
			exit(1);
		}

		connList.push_back(con);
		++FreeConn;
	}

	this->MaxConn = MaxConn;
	this->UsedConn = 0;
	pthread_mutex_unlock(&lock);
}

my_Pool* my_Pool::GetInstance(string IP,int Port,string User,string PassWord,string DBName,unsigned int MaxConn)
{
	if(connPool == NULL)
	{
		connPool = new my_Pool(IP,Port,User,PassWord,DBName,MaxConn);
	}

	return connPool;
}

MYSQL* my_Pool::GetConnection()
{
	MYSQL * con = NULL;
	pthread_mutex_lock(&lock);

	if(connList.size() > 0)
	{
		con = connList.front();
		connList.pop_front();
		
		--FreeConn;
		++UsedConn;
		
		pthread_mutex_unlock(&lock);
		return con;
	}

	return NULL;
}

bool my_Pool::ReleaseConnection(MYSQL * con)
{
	pthread_mutex_lock(&lock);
	if(con != NULL)
	{
		connList.push_back(con);
		++FreeConn;
		--UsedConn;

		pthread_mutex_unlock(&lock);
		return true;
	}
	
	return false;
}

void my_Pool::DestoryPool()
{
	pthread_mutex_lock(&lock);
	if(connList.size() > 0)
	{
		list<MYSQL *>::iterator it;
		for(it = connList.begin(); it != connList.end(); ++it)
		{
			MYSQL * con = *it;
			mysql_close(con);
			connList.pop_front();
		}
		UsedConn = 0;
		FreeConn = 0;
		connList.clear();
	}
}

int my_Pool::GetFreeConn()
{
	return this->FreeConn;
}

my_Pool::·my_Pool()
{
    DestoryPool();
}

/*************************************************************************
	> File Name: Test_Pool.cpp
	> Author: LCG
	> Mail: 
	> Created Time: Fri Apr 29 16:31:41 2016
 ************************************************************************/

#include"my_Pool.h"
#include<mysql.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>

using namespace std;


int main(void)
{
	 my_Pool *conn = my_Pool::GetInstance("ipaddress",port,"username","password","dbname",10);
	if(conn != 0)
	{
		cout<<"get connection success"<<endl;
	}
	else
	{
		cout<<"get error"<<endl;
	}

	cout<<"conn = "<<conn->GetFreeConn()<<endl;

	MYSQL * con = conn->GetConnection();
	
	const char *query = "select  * from tablename ";
	if(mysql_query(con,query))
	{
		cout<<"Failed to query data"<<endl;
		exit(1);
	}

	MYSQL_RES *result;
	MYSQL_ROW row;
	int numField = 0;

	result = mysql_use_result(con);
	for(int i = 0; i < mysql_field_count(con);++i)
	{
		row = mysql_fetch_row(result);
		if(row <= 0)
		{
			break;
		}
		for(int j = 0; j < mysql_num_fields(result);++j)
		{
			cout<<row[i]<<" ";
		}
		cout<<endl;
	}

	mysql_free_result(result);
	return 0;
}


转载于:https://my.oschina.net/lcg521521/blog/668249

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值