您的位置:首页 > 编程语言 > C语言/C++

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

2016-04-29 00:00 302 查看
摘要: C++ 连接池

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

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql 连接池 C++