当前位置:网站首页>数据库连接池:连接池功能点的实现
数据库连接池:连接池功能点的实现
2022-06-22 13:39:00 【_索伦】
连接池功能
主要包含以下功能:
- 单例模式的连接池,构造函数需私有化
- 需要一个获取连接的函数
- 加载配置文件的函数
- 生产者线程,用来生产连接
- 用来回收多余空闲连接的函数,独立的线程
数据成员有:
- 数据库所需信息
- 连接池的初始连接量和最大连接量
- 连接池的最大等待时间
- 连接池获取连接的超时时间
- 存储MySQL连接的队列
- 维护该连接队列的互斥量
- 原子整型操作,记录连接个数
- 条件变量,用于生产者和消费者线程之间的通信。
图示:
因此可编写出头文件信息:
连接池头文件
#pragma once
// 实现连接池功能模块
#include<iostream>
#include<string>
#include<queue>
#include<mutex>
#include<atomic>
#include<thread>
#include<memory>
#include<functional>
#include<condition_variable>
#include "Connection.h"
using namespace std;
class ConnectionPool
{
public:
// 获取连接池对象实例 静态的
static ConnectionPool* getConnectionPool();
// 给外部提供接口,提供一个空闲的连接
shared_ptr<Connection> getConnection();
private:
ConnectionPool(); // 单例模式 构造函数私有化
bool loadConfigFile(); // 加载配置文件
// 生产者线程函数, 负责生产新连接
void produceConnectionTask();
// 回收线程函数,负责回收多余空闲连接
void scannerConnectionTask();
string _ip; // MySQL的IP地址
unsigned short _port; // MySQL的端口号
string _username; // MySQL的登陆用户名
string _password; // MySQL的登录密码
string _dbname; // 数据库名字
int _initSize; // 连接池的初始连接量
int _maxSize; // 连接池的最大连接量
int _maxIdleTime; // 连接池的最大等待时间
int _connectionTimeOut; // 连接池获取连接的超时时间
queue<Connection*> _connectionQue; // 存储MySQL连接的队列
mutex _queueMutex; // 维护连接队列的线程安全互斥锁
atomic_int _connectionCnt; // 记录连接所创建的connection的总连接数,不能超过_maxSize
condition_variable cv; // 连接生产线程和消费线程之间的通信
};
功能实现
单例模式的连接池
这里采用懒汉单例模式,由编译器自己进行加锁操作
// 线程安全的懒汉单例模式函数接口
ConnectionPool* ConnectionPool::getConnectionPool()
{
// 静态局部变量,编译器自动lock和unlock
static ConnectionPool pool;
return& pool;
}
MySQL配置文件
在Windows下该文件的后缀为 MySQL.ini
# 数据库连接池的配置文件
ip=127.0.0.1
port=3306
username=root
password=123456
dbname=chat
initSize=10
maxSize=1024
# 最大空闲时间默认是秒
maxIdleTime=60
# 连接超时时间,单位是毫秒
connectionTimeOut=100
读取配置文件
// 从配置文件中加载配置项
bool ConnectionPool::loadConfigFile()
{
FILE* pf = fopen("mysql.ini", "r");
if (pf == nullptr)
{
LOG("mysql.ini file is not exist!");
return false;
}
while (!feof(pf))
{
char line[1024] = {
0 };
fgets(line, 1024, pf);
string str = line;
int idx = str.find('=', 0);
if (-1 == idx) // 无效的配置
{
continue;
}
int endidx = str.find('\n', idx);
string key = str.substr(0, idx);
string value = str.substr(idx + 1, endidx - idx - 1);
if (key == "ip")
{
_ip = value;
}
else if (key == "port")
{
_port = atoi(value.c_str());
}
else if (key == "username")
{
_username = value;
}
else if (key == "password")
{
_password = value;
}
else if (key == "dbname")
{
_dbname = value;
}
else if (key == "initSize")
{
_initSize = atoi(value.c_str());
}
else if (key == "maxSize")
{
_maxSize = atoi(value.c_str());
}
else if (key == "maxIdleTime")
{
_maxIdleTime = atoi(value.c_str());
}
else if (key == "connectionTimeOut")
{
_connectionTimeOut = atoi(value.c_str());
}
}
return true;
}
生产者线程实现
// 生产者:运行在独立的线程,专门负责生产连接
void ConnectionPool::produceConnectionTask()
{
// 循环(一直再监听)
for (; ;)
{
unique_lock<mutex> lock(_queueMutex);
while (!_connectionQue.empty())
{
cv.wait(lock); // 队列不空,生产线程进入条件变量的等待队列
}
// 连接数量没有到达上限,继续创建新的连接
if (_connectionCnt < _maxSize)
{
Connection* p = new Connection();
p->connect(_ip, _port, _username, _password, _dbname);
p->refreshAliveTime(); // 刷新一下开始空闲的起始时间
_connectionQue.push(p);
_connectionCnt++;
}
// 唤醒等待队列里的所有线程
cv.notify_all(); // 通知消费者线程, 可以消费连接了
}
}
消费者功能实现
// 给外部提供接口,提供一个空闲的连接
shared_ptr<Connection> ConnectionPool::getConnection()
{
unique_lock<mutex> lock(_queueMutex);
while (_connectionQue.empty())
{
// 不是直接sleep
// 超时醒来发现还是空的,就返回nullptr
if (cv_status::timeout == cv.wait_for(lock, std::chrono::milliseconds(_connectionTimeOut)))
{
if (_connectionQue.empty())
{
LOG("获取空闲连接超时了...获取连接失败!");
return nullptr;
}
}
}
/* 由于shared_ptr析构的时候,会把Connection的资源直接delete掉, 相当于调用了Connection的析构函数,Connection就被close掉了, 所以这里需要自定义智能指针释放资源的方式,改用将资源还到队列里 */
shared_ptr<Connection> sp(_connectionQue.front(),
[&](Connection* pcon) {
// 这里是在服务器应用线程中调用的,需要考虑线程安全
unique_lock<mutex> lock(_queueMutex);
pcon->refreshAliveTime(); // 刷新一下开始空闲的起始时间
_connectionQue.push(pcon);
});
_connectionQue.pop();
// 消费完队列里最后一个Connection,就通知生产者线程生产连接
cv.notify_all();
return sp;
}
回收多余连接,独立线程
这里需要MySQL连接的类里实现两个函数:
clock_t _aliveTime; // 记录进入空闲状态后的存活时间
// 刷新一下连接的起始空闲时间点
void Connection::refreshAliveTime()
{
_aliveTime = clock();
}
// 返回存活的时间
clock_t Connection::getAliveTime() const
{
return clock() - _aliveTime;
}
然后再来看回收函数
// 扫描超过maxIdleTime的空闲连接,进行多余的连接回收
void ConnectionPool::scannerConnectionTask()
{
for (; ;)
{
// 通过sleep模拟定时效果
this_thread::sleep_for(std::chrono::seconds(_maxIdleTime));
// 扫描整个队列,释放多于连接
unique_lock<mutex> lock(_queueMutex);
while (_connectionCnt > _initSize)
{
Connection* p = _connectionQue.front();
if (p->getAliveTime() >= (_maxIdleTime * 1000))
{
_connectionQue.pop();
_connectionCnt--;
delete p; // 调用 ~Connection();
}
else
{
break; // 队头的连接没有超过_maxIdleTime,后面的肯定没有
}
}
}
}
边栏推荐
- flutter video_player實現監聽和自動播放下一首歌曲
- 谷歌竞价账户可以探测到全球市场吗?
- C语言学生管理系统(开源)
- C # paging calculation total pages, current page data set
- Unity's rich text color sets the color to be fully transparent
- We will resolutely fight and win the hard battle of flood control and disaster relief and spare no effort to ensure the safety of people's lives and property
- U++编程 移动 学习笔记
- Verification code is the natural enemy of automation? See how the great God solved it
- Groovy list operation
- 一文彻底弄懂单例模式(Singleton)
猜你喜欢

Zhongshanshan: engineers after being blasted will take off | ONEFLOW u

看完這篇 教你玩轉滲透測試靶機Vulnhub——DriftingBlues-5

RealNetworks vs. Microsoft: the battle in the early streaming media industry

visual studio开发过程中常见操作
![[live broadcast review] battle code pioneer phase VI: build a test subsystem and empower developers to provide](/img/46/d36ae47c3d44565d695e8ca7f34980.jpg)
[live broadcast review] battle code pioneer phase VI: build a test subsystem and empower developers to provide

How location coding (PE) works in transformers

那些令人懵逼的用户态&内核态

Deadlock found when trying to get lock; Try restarting transaction

【考研攻略】北京交通大学网络空间安全专业2018-2022年考研数据分析

开源SPL重新定义OLAP Server
随机推荐
机器学习之支持向量机
一文彻底弄懂单例模式(Singleton)
扩散模型又杀疯了!这一次被攻占的领域是...
一文彻底弄懂工厂模式(Factory)
Is the encryption market a "natural disaster" or a "man-made disaster" in the cold winter?
Unity prevents the BTN button from being clicked continuously
How maxscale handles event status after MariaDB master-slave switchover -handle_ events
CSAPP之详解Labs
Thoroughly understand the builder mode (builder)
C # define and implement interface interface
Kukai TV ADB
Sikulix选取相对位置的图片(进阶版)
UE4 通过蓝图获取本地文件
Thoroughly understand the factory mode
Groovy之列表操作
Phpstudy 2016 build Pikachu range
S7-200SMART与FANUC机器人进行Profinet通信的具体方法和步骤
How to use SQL to modify in the database & delete
Struggle, programmer chapter 45 tenderness is like water and a good time is like a dream
本周四晚19:00战码先锋第7期直播丨三方应用开发者如何为开源做贡献