一、头文件【存为:connPool.h】

#ifndef __CONNECTION_POOL_H__

#define __CONNECTION_POOL_H__

#include "mutex.h"

#define MYSQL_CONN_NUM_MAX_VALUE 500

using namespace std;

enum _USE_STATUS

{

US_USE = 0,

US_IDLE = 1

};

typedef struct _sConStatus

{

void* connAddr;

int useStatus;

}sConStatus;

class CConnPool

{

public:

CConnPool();

~CConnPool();

public:

int Init(string& strMysqlIp, string& strUser, string& strPwd, string& strDbName, int nMysqlPort, int nConnNum);//connection pool init

void* getOneConn();//get a connection

void retOneConn(void* pMysql);// return a connection

void checkConn(); // check the connection if is alive

void* createOneConn();

public:

char m_szMysqlIp[100];

char m_szUser[100];

char m_szPwd[100];

char m_szDbName[100];

int m_nMysqlPort; int m_nConnNum;

public:

CMutex m_sMutex;

vector m_vectorConn;

map m_mapVI;

map m_mapMysqlScs;

};

class CConnPoolV2

{

public:

CConnPoolV2();

~CConnPoolV2();

public:

int Init(string& strMysqlIp, string& strUser, string& strPwd, string& strDbName, int nMysqlPort, int nConnNum);//connection pool init

void* getOneConn(); //从连接池取一个连接

void retOneConn(void* pConn);// 连接用完了,把它放回连接池。以便其他人用。

void checkConn(); // check the connection if is alive

void* createOneConn();

private:

string m_strMysqlIp;

string m_strUser;

string m_strPwd;

string m_strDbName;

int m_nMysqlPort; int m_nConnNum;

private:

CMutex m_sMutex;

vector m_vectorConn;

map m_mapVI; // 从连接的地址,快速找到索引,便于存放到m_vectorConn中。

};

#endif

二、源码【存为:connPool.cpp】

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include  #include  #include

#include

#include

#include

#include "mysql.h"

#include "encapsulation_mysql.h"

#include "connPool.h"

#include "mutex.h"

using namespace std;

using namespace EncapMysql;

CConnPool::CConnPool( )

{

}

CConnPool::~CConnPool( )

{

}

void* CConnPool::createOneConn()

{

MYSQL* mysql;

mysql = mysql_init(0);

if(mysql == NULL)

{

cout << "mysql_init fail**" << endl; return NULL;

}

if(mysql_real_connect(mysql, m_szMysqlIp , m_szUser , m_szPwd, m_szDbName , m_nMysqlPort, NULL,0)==NULL)

{

cout << "connect failure!" << endl;

return NULL;

}

else

{

cout << "connect success!" << endl;

}

//

return mysql;

}

int CConnPool::Init(string& strMysqlIp, string& strUser, string& strPwd, string& strDbName, int nMysqlPort, int nConnNum)

{

strcpy(m_szMysqlIp, strMysqlIp.c_str());

strcpy( m_szUser, strUser.c_str());

strcpy(m_szPwd, strPwd.c_str());

strcpy(m_szDbName, strDbName.c_str());

m_nMysqlPort = nMysqlPort; m_nConnNum = nConnNum; MYSQL* mysql;

for(int i=0; i

{

mysql = (MYSQL*)this->createOneConn();

if(mysql == NULL)

return -1;

// sConStatus* scs = new sConStatus();

scs->connAddr = mysql;

scs->useStatus = US_IDLE;

m_vectorConn.push_back(scs); m_mapVI[scs] = i;

m_mapMysqlScs[mysql] = scs;

}

m_nConnNum = nConnNum;

}

//从连接池中取一个连接,同时,给它做一个标记,表明它已经被使用,防止别的线程再使用。

void* CConnPool::getOneConn()

{

int N = m_vectorConn.size();

for(int i=0; i< N; i++)

{

CGuard guard(m_sMutex);

sConStatus* scs = (sConStatus*)m_vectorConn[i];

if(scs->useStatus == US_IDLE)

{

scs->useStatus = US_USE;

return scs->connAddr;

} }

//

return NULL;

}

//把连接归还给连接池。同时,给它做一个标记,表明它是空闲的,可以使用。

void CConnPool::retOneConn(void* pMysql)

{

if(!pMysql)

return;

// map::iterator it1;

map::iterator it2;

CGuard guard(m_sMutex);

it1 = m_mapMysqlScs.find(pMysql);

if(it1 == m_mapMysqlScs.end())

return;

it2 = m_mapVI.find(it1->second);

if(it2 == m_mapVI.end())

return;

int nInx = it2->second;

sConStatus* scs = (sConStatus*) m_vectorConn[nInx];

scs->useStatus = US_IDLE;

}

void CConnPool::checkConn()

{

map::iterator it1;

MYSQL* mysql;

// for(int i=0; i

{

CGuard guard(m_sMutex);

sConStatus* scs = (sConStatus*)m_vectorConn[i];

if(scs->useStatus == US_USE)

continue;

// mysql =(MYSQL*)(scs->connAddr);

int status=mysql_query(mysql, "select count(*) from t_user;" );

if(status != 0) //说明连接已经不可用了。

{

it1 = m_mapMysqlScs.find(mysql);

if(it1 != m_mapMysqlScs.end())

{

m_mapMysqlScs.erase(it1);

}

//

mysql_close(mysql);

//

mysql = (MYSQL*)this->createOneConn();

m_mapMysqlScs[mysql] = scs;

}

}

//

}

// 2011-01-20, 这个类这样写,感觉耦合性更为松散,比较好。使用起来也好理解一些。

CConnPoolV2::CConnPoolV2( )

{

}

CConnPoolV2::~CConnPoolV2( )

{

}

//创建一个连接,并设为 IDLE状态。

void* CConnPoolV2::createOneConn()

{

try

{

CEncapMysql* pEM = new CEncapMysql();

if(pEM == NULL)

{

printf("pEM == NULL**\r\n"); return NULL;

} //

int nRet = pEM->Connect(m_strMysqlIp.c_str(), m_strUser.c_str(), m_strPwd.c_str());

if(nRet != 0)

{

printf("pEM->Connect fail**\r\n"); return NULL;

} // pEM->SetIdle();

//

return pEM;

} catch(...)

{

printf("createOneConn exception**\r\n"); return NULL;

}

}

//成功: 返回0

int CConnPoolV2::Init(string& strMysqlIp, string& strUser, string& strPwd, string& strDbName, int nMysqlPort, int nConnNum)

{

m_strMysqlIp = strMysqlIp;

m_strUser = strUser;

m_strPwd = strPwd;

m_strDbName = strDbName;

m_nMysqlPort = nMysqlPort; m_nConnNum = nConnNum; CEncapMysql* pEM;

int nRet;

for(int i=0; i

{

pEM = (CEncapMysql*)this->createOneConn();

if(!pEM )

return -1;

// m_vectorConn.push_back(pEM); m_mapVI[pEM] = i;

}

return 0;

}

void* CConnPoolV2::getOneConn()

{

CGuard guard(m_sMutex);

//

for(int i=0; i< m_nConnNum; i++)

{

CEncapMysql* pEM = (CEncapMysql*)m_vectorConn[i];

if( pEM->IsIdle())

{

pEM->SetUsed();

return pEM;

} }

//可能访问MYSQL的用户较多,连接池中已无空闲连接了。只要总连接数没有超限,就新建一个连接。

if(m_nConnNum < MYSQL_CONN_NUM_MAX_VALUE)

{

CEncapMysql* pEM = (CEncapMysql*)this->createOneConn();

if(!pEM )

return NULL;

// m_vectorConn.push_back(pEM); m_mapVI[pEM] = m_nConnNum++;

} //

return NULL;

}

void CConnPoolV2::retOneConn(void* pConn)

{

map::iterator it;

CGuard guard(m_sMutex);

it = m_mapVI.find(pConn);

if(it == m_mapVI.end())

{

printf("retOneConn fail***\n"); return;

} int nInx = it->second;

CEncapMysql* pEM = (CEncapMysql*) m_vectorConn[nInx];

pEM->SetIdle();

printf("retOneConn succ!\n"); }

void CConnPoolV2::checkConn()

{

//暂时可以不实现。因为查询失败时,已重新连接了。

}

c mysql连接池_在LINUX下用C/C++写了一个连接池(访问MYSQL)的类相关推荐

  1. c语言中怎么暂停一个一个游戏,求助:最近在linux下用c语言写了一个贪吃蛇程序,有几个问题,第一:贪吃蛇怎么实现暂停,第二:有时候同时输入上下左右中的两个键就会直接游戏结束...

    求助:最近在linux下用c语言写了一个贪吃蛇程序,有几个问题,第一:贪吃蛇怎么实现暂停,第二:有时候同时输入上下左右中的两个键就会直接游戏结束 /* 以下是主要的逻辑代码,还有些.c和.h就没发了 ...

  2. linux mysql 备份 恢复_[转]linux下如何备份与恢复mysql数据库。

    数据库备份是非常重要的.如果定期做好备份,这样就可以在发生系统崩溃时恢复数据到最后一次正常的状态,把损失减小到最少. 一. 用命令实现备份 MySQLl提供了一个mysqldump命令,我们可以用它进 ...

  3. linux下导入mysql表乱码_在linux下导入.sql文件,数据库中文乱码

    现象描述 我是在aix下面导入如下SQL语句时,数据库中显示乱码. insert into CONFERENCE(CONFERENCEID,SUBCONFERENCEID,ACCESSNUMBER,A ...

  4. Linux下安装DM数据库及SrpingBoot+druid连接DM数据库

    本文安装使用 CentOS7 操作系统,为 x86_64 架构,安装步骤全部以命令行方式,安装过程为参考达梦官方文档后的总结.安装完成后使用SrpingBoot+druid连接DM数据库,使用Mave ...

  5. Linux下通过mail发送qq邮件出现连接超时的情况

    Linux下通过mail发送qq邮件出现连接超时的情况 一.一般来说都是smtp的地址配置错误: 之前配置过的地址有: 1) set smtp=smtp.qq.com 2) set smtp=smtp ...

  6. mysql c测试程序_Linux平台下从零开始写一个C语言访问MySQL的测试程序

    Linux 平台下从零开始写一个 C 语言访问 MySQL 的测试程序 2010-8-20 Hu Dennis Chengdu 前置条件: (1) Linux 已经安装好 mysql 数据库: (2) ...

  7. linux下分区ntfs,简易教程:Linux下NTFS分区的写操作

    Linux下NTFS分区的写操作只需通过简单点击即可完成. 在你正常的工作中,假如你装的是双系统,其中一个是Winodws系统,而你又在Linux环境下办公,需要用到Windows分区中的某文档资料或 ...

  8. linux下cp复制目录时排除某些目录的方法分享,Linux 下复制(cp)目录时排除一个或者多个目录的方法...

    cp 貌似没有排除目录的功能,可以使用 rsync 命令来实现了,如: [案例] /home/52php目录里面有data目录,data目录里面有 a.b.c.d.e 五个目录,现在要把data目录里 ...

  9. linux设置mysql定时任务_原创 Linux下实现Mysql定时任务备份数据

    本实例将创建目录放置于/mnt目录下,可根据具体情况放置于其他目录: cd /mnt mkdir dbback pwd /mnt/dbback 创建shell脚本 脚本名称可根据自己规范进行自定义: ...

最新文章

  1. 字节跳动小程序技术摘要
  2. w10查询自己电脑ip
  3. C++Primer第五版学习笔记
  4. 用dreamweaver cs6快速布局后台架构_后台系统:产品设计 | 七步法
  5. go反射机制与类型识别
  6. 复习 Python 匿名函数 内建函数
  7. 第十三讲 ASP.NET中的错误处理和程序优化
  8. mysql 储存过程放到哪_MySQL储存过程
  9. c语言作业及参考答案,C语言试题及答案
  10. Java JSch 远程执行 Shell 命令
  11. ExoPlayer的缓存 四 缓存Cache 的应用
  12. Calendar获取当天0点的时间戳
  13. Atitit q2016 q0 doc list on home ntpc.docx
  14. matlab设计椭圆低通滤波器,【 MATLAB 】ellip 函数介绍(椭圆滤波器设计)
  15. ubuntu安装redis并使用
  16. java long初始化_java中long类型的变量想要初始化其值为空,要怎么实现?
  17. HTML网页设计结课作业~仿蘑菇街商城网站源码(HTML+CSS+JS)
  18. 论文速递 EMNLP2022 | 接受论文抢先看!!!(内含下载列表)
  19. 商标图形也会侵权?商标侵权如何界定?
  20. Going out on a limb:Joint Extraction of Entity Mentions and Relations without Dependency Trees【论文笔记】

热门文章

  1. where is flag GV_IMMEDIATE_RESTART marked in SAP CRM WebClient UI
  2. /ui2/nwbc_nav_tr /ui2/cl_nwbc_runtime35~check_navigation_tree_cache
  3. SAP UI5 jQuery.sap.setObject
  4. SAP UI5 OData框架里硬编码的80是怎么来的
  5. 使用postman消费Marketing Cloud的contact读取API
  6. CordovaWebViewImpl
  7. IBASE change related BDOC generation
  8. 避免CRM_IB 013 error message
  9. SAP WebIDE里UI5应用的隐藏文件project.json
  10. SAP One Order redesign里的WebUI advanced search重构