对于一个简单的数据库应用,对于数据库的访问不是很频繁,这时在需要访问数据库时,新创建一个连接,用完后就关闭它,这样做也不会带来什么明显的性能上的开销。但是对于一个复杂的数据库应用,情况就完全不同了,频繁的建立、关闭连接,会极大的减低系统的性能,因为对于连接的使用成了系统性能的瓶颈。数据库连接池的基本原理是在内部对象池中维护一定数量的数据库连接,并对外暴露数据库连接获取和返回方法。用户使用完后将连接返回,此时连接并没有关闭,而是由连接池管理器回收,并为下一次使用做好准备。

数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个;释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏。。这项技术能明显提高对数据库操作的性能。

mysqlpp::ConnectionPool

此类主要维护一个双向链表std::list PoolT,结构体内容包含:连接对象,是否在使用,上次使用时间,既然是链表就很容易的知道连接池当前的连接数及对连接增加删除操作

virtual Connection* exchange(const Connection* pc); //

virtual Connection* grab();              //从连接池获取一个连接,如果没有则创建一个

Connection* safe_grab();                //从连接池获取一个可用的连接

virtual void release(const Connection* pc);      //将一个连接置为未使用状态

void shrink()                      //从连接池删除未使用的连接

连接池的使用

mysqlpp::ScopedConnection

通过构造函数从连接池中获取一个Connection,析构函数将Connection对象重新放到连接池。

此类中维护了一个指向Connection指针,可以执行Connection的操作

MysqlConnectPool.h

#ifndef _MYSQL_CONNECT_POOL_H_#define _MYSQL_CONNECT_POOL_H_#include#include

class MysqlConnectPool : publicmysqlpp::ConnectionPool

{public:

MysqlConnectPool(std::stringdbname,

std::stringserverip,

std::stringuser,

std::stringpasswd,intport,

std::stringcharset)

:m_dbname(dbname)

,m_server_ip(serverip)

,m_user(user)

,m_password(passwd)

,m_charset(charset)

,m_port(port)

{

m_max_idle_time= 300;

}virtual ~MysqlConnectPool()

{

clear();

}const std::string& getDBName() const { returnm_dbname; }const std::string& getServerIP() const { returnm_server_ip; }const std::string& getUser() const { returnm_user; }const std::string& getPassword() const { returnm_password; }const std::string& getCharset() const { returnm_charset; }int getPort() const { returnm_port; }void setMaxIdleTime(intmax_idle)

{

m_max_idle_time=max_idle;

}virtual mysqlpp::Connection*grab()

{returnmysqlpp::ConnectionPool::grab();

}virtual void release(const mysqlpp::Connection*pc)

{

mysqlpp::ConnectionPool::release(pc);

}protected:virtual mysqlpp::Connection*create()

{

mysqlpp::Connection* conn = new mysqlpp::Connection(true);

mysqlpp::SetCharsetNameOption* pOpt = newmysqlpp::SetCharsetNameOption(m_charset.c_str());

conn->set_option( pOpt );

conn->connect(m_dbname.empty()? 0: m_dbname.c_str(),

m_server_ip.empty()? 0: m_server_ip.c_str(),

m_user.empty()? 0: m_user.c_str(),

m_password.empty()? "": m_password.c_str(),

m_port);returnconn;

}virtual void destroy(mysqlpp::Connection*cp)

{deletecp;

}virtual unsigned intmax_idle_time()

{returnm_max_idle_time;

}private:

std::stringm_dbname;

std::stringm_server_ip;

std::stringm_user;

std::stringm_password;

std::stringm_charset;intm_port;intm_max_idle_time;

};#endif

#include #include#include#include#include#include#include#include"MysqlConnectPool.h"std::shared_ptr db_pool =nullptr;

std::string db_name = "test";

std::string db_ip = "127.0.0.1";

std::string db_user = "root";

std::string db_passwd = "123456";int db_port = 3306;

std::string db_charset = "utf8";intmain()

{

db_pool= std::make_shared(db_name, db_ip, db_user, db_passwd, db_port, db_charset);for (int i = 0; i < 10; i++)

{try{

mysqlpp::ScopedConnection conn(*db_pool);

mysqlpp::Query query= conn->query();

//................................}catch (const mysqlpp::Exception&e) {

std::cout<< "error:" << e.what() << "\n";return false;

}

}return 0;

}

mysql connec_mysql++ Connect相关推荐

  1. mysql 的connect 设置 无法点next_技术分享 | MySQL 使用 MariaDB 审计插件

    作者:姚远 专注于 Oracle.MySQL 数据库多年,Oracle 10G 和 12C OCM,MySQL 5.6 ,5.7,8.0 OCP.现在鼎甲科技任顾问,为同事和客户提供数据库培训和技术支 ...

  2. nodejs连接mysql报connect ECONNREFUSED错误的解决方法

    通过AMH面板安装的mysql,用nodejs连接mysql时候结果报[connect ECONNREFUSED]连接错误. 解决方法:添加socketPath 有些mysql 安装的时候不是mysq ...

  3. Solving a “communications link failure” with jdbc and mysql :Cannot connect to database server Commu

    当出现: Cannot connect to database server Communications link failure 错误时,可以考虑下面的文章: http://stackoverfl ...

  4. mysql 实现 connect by start with

    一 前言 1.mysql没有层级查询方法 而 oracle通过connect by  start with语法可以实现层级查询 2.mysql实现层级查询的方式很多,有使用存储过程函数嵌套调用亦有使用 ...

  5. 安装 Python MySQL 驱动(mysql-connector-python、MySQL-python)

    1. 安装 由于 MySQL 服务器以独立的进程运行,并通过网络对外服务,所以,需要支持 Python 的MySQL 驱动来连接到 MySQL 服务器. 目前,有两个MySQL驱动: mysql-co ...

  6. mysql connector python linux_MySQL Connector/Python 安装、测试

    安装Connector/Python: # wget http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-1. ...

  7. Mysql 基于 Amoeba 的 读写分离(2)

    <?xml version="1.0" encoding="gbk"?> <!DOCTYPE amoeba:configuration SYS ...

  8. mysql分库分表分页查询语句_MySQL分库分表分库后的查询(8th)

    前言 这边我们以使用python程序要展示一下再分库分表后,我们需要如何对数据库进行操作. python操作数据库 我们这边还是沿用之前的那5中:场景1:购买者下订单#!/usr/bin/env py ...

  9. python连接MySQL并进行数据查询

    python连接MySQL并进行数据查询 #建立数据库的连接 mydb = mysql.connector.connect(host="0.0.0.0",user="ro ...

最新文章

  1. Decode()函数使用技巧
  2. 基于深度学习的多目标跟踪算法——ReID与MOT的联系
  3. yum安装apache及问题解决
  4. java php python 高并发_关于php如何调用Python快速发送高并发邮件的示例代码
  5. 英特尔表示:元宇宙的路还很长
  6. 解决sqlalchemy连接mysql报错ModuleNotFoundError: No module named ‘pymysql‘
  7. k近邻法的实现(kd树)-相关问题梳理
  8. Flutter视图基础简介--Widget、Element、RenderObject
  9. 5 . 2 查 询 优 化 器
  10. 【Python-2.7】多种方式删除列表元素
  11. 四川大学软件学院操作系统笔记
  12. html点击超链接出现弹窗,如何实现超链接弹窗打开
  13. 玩转AgileCDN(二)——运维小哥做报表,不再为时区换算而烦恼
  14. PMP考纲解读 |【人】任务3—支持团队绩效(二)
  15. 虚拟机2008安装DHPC服务器,Windows Server 2008 配置DHCP服务器
  16. GMS:基于网格运动统计的快速极度鲁棒的特征匹配
  17. 集合20210801
  18. closest() 方法
  19. AD软件——把原理图库 和 PCB元件库封装模型 关联起来
  20. 图解LeetCode——592. 分数加减运算(难度:中等)

热门文章

  1. Employees数据库的导入
  2. 【JOISC 2020 Day3 T2】 Harvest
  3. 设计师如何明确设计目标?
  4. 用了Mounty后移动硬盘没有初始化
  5. # Hello Word
  6. 跨境电商是拉动外贸经济的王牌-扬帆际海
  7. 一张图看懂Kafka应用
  8. 重磅!Creator 3.0 3D跑酷游戏,连载视频教程!
  9. 视频教程 | 3D 跑酷小游戏实战开发(下)
  10. 互联先锋法国云服务器 为外贸企业带来福音