连接池代码和配置文件
连接池配置文件mysql.ini:
#数据库连接池的配置文件
ip=127.0.0.1
port=3306
username=root
password=123456
dbname=score
initSize=10
maxSize=1024
#最大空闲时间单位是秒
maxIdleTime=60
#连接超时时间单位是毫秒
connectionTimeOut=100
连接池头文件CommomConnectionPool.h:
#pragma once
/*
实现连接池功能模块
*/
#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();
//扫描多余的空闲连接,超过maxIdleTime时间的空闲连接,进行多余的连接回收
void scannerConnectionTask();
string _ip; //mysql ip地址
unsigned short _port; //mysql端口号 3306
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连接的总数量
condition_variable cv; //设置条件变量,用于连接生产线程和连接消费线程的通信
};
CommomConnectionPool.cpp:
#include "pch.h"
#include "CommonConnectionPool.h"
#include "public.h"
//线程安全的懒汉单例接口
ConnectionPool* ConnectionPool::getConnectionPool() {
static ConnectionPool pool;
return &pool;
}
//从配置文件加载配置项
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 (idx == -1) {
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") {
int _connectionTimeOut = atoi(value.c_str());
}
}
return true;
}
ConnectionPool::ConnectionPool() {
//加载配置项
if (!loadConfigFile()) return;
//创建初始数量的连接
for (int i = 0; i < _initSize; i++) {
Connection* p = new Connection;
p->connect(_ip, _port, _username, _password, _dbname);
p->refreshAliveTime();
_connectionQue.push(p);
_connectionCnt++;
}
//启动一个线程作为生产者线程
thread produce(std::bind(&ConnectionPool::produceConnectionTask, this));
produce.detach();
//启动一个新的线程,扫描多余的空闲连接,超过maxIdleTime时间的空闲连接,进行多余的连接回收
thread scanner(std::bind(&ConnectionPool::scannerConnectionTask, this));
scanner.detach();
}
void ConnectionPool::produceConnectionTask() {
while (1) {
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()) {
if (cv_status::timeout == cv.wait_for(lock, chrono::milliseconds(_connectionTimeOut))) {
if (_connectionQue.empty()) {
LOG("获取空闲连接超时...获取连接失败!");
return nullptr;
}
}
}
shared_ptr<Connection> sp(_connectionQue.front(),
[&](Connection* pcon) {
unique_lock<mutex> lock(_queueMutex);
pcon->refreshAliveTime();
_connectionQue.push(pcon);
});
_connectionQue.pop();
cv.notify_all(); // 消费完连接以后,通知生产者线程检查一下,如果队列为空了,赶紧生产连接
return sp;
}
void ConnectionPool::scannerConnectionTask() {
while (1) {
//通过sleep模拟定时效果
this_thread::sleep_for(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;
}
else {
break;
}
}
}
}
调试输出public.h:
#pragma once
#include <iostream>
using namespace std;
#define LOG(str)\
cout << __FILE__ << " : " << "__LINE__" << " " << \
__TIMESTAMP__ << " : " << str << endl;