From e34d5b85cf60ea14f7433440fa4aa6f90414819f Mon Sep 17 00:00:00 2001 From: Administrator <3174665893@qq.com> Date: 星期六, 09 十一月 2024 11:24:16 +0800 Subject: [PATCH] 修改结构体 --- Server/王琨元/code/ConnectionPool.h | 84 ++++++++++++++++++----------------------- 1 files changed, 37 insertions(+), 47 deletions(-) diff --git "a/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.h" "b/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.h" index 3ad1ee0..c78a46e 100644 --- "a/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.h" +++ "b/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.h" @@ -1,54 +1,44 @@ #pragma once + #include "MysqlConn.h" -#include "./tinyxml/tinyxml.h" -#include <queue> -#include <mutex> -#include <thread> -#include <atomic> -#include <condition_variable> - -#pragma comment(lib, "./tinyxml/x64/Debug/tinyxml.lib") - -// 应用-单例模式:懒汉模式[需要考虑多线程安全问题] class ConnectionPool { -private: - ConnectionPool(); - // 移动拷贝最终还是有且仅有一个对象,所以依旧是属于单例模式。 - // delete 阻止拷贝构造和拷贝赋值的类对象生成 - ConnectionPool(ConnectionPool&) = delete; - ConnectionPool& operator=(ConnectionPool&) = delete; - ~ConnectionPool(); - // 解析xml配置文件 读取数据库及连接池的相关信息 - bool parseXmlFile(); - // 添加连接 - bool addConnection(); - // 线程函数 - void productConnection(); - void recycleConnection(); - - // 存放数据库连接池建立的连接 - queue<MysqlConn*> m_connections; - // 一些基本信息 - string m_ip; // IP - unsigned short m_port; // 端口 - string m_user; // 用户名 - string m_passwd; // 密码 - string m_dbName; // 数据库名称 - int m_minSize; // 初始连接量(最小连接量) - int m_maxSize; // 最大连接量 - int m_timeout; // 超时时长 - int m_maxIdleTime; // 最大空闲时长 - // 线程安全相关 - mutex m_mutex; - condition_variable m_cond; // 仅用一个条件变量来唤醒线程,但并不影响线程运行 - condition_variable m_cond1; - // 连接数量 - atomic_int m_num; // 连接的总数量 public: - // 获取单例对象的接口 - static ConnectionPool* getConnectPool(); - // 用户获取连接的接口, 如果获取失败,会返回nullptr - shared_ptr<MysqlConn> getConnection(); + static ConnectionPool* getInstance(); + + shared_ptr<MysqlConn> getMysqlConn(); // 从数据库连接池获取连接 + + ConnectionPool(const ConnectionPool& other) = delete; + ConnectionPool& operator=(const ConnectionPool& other) = delete; + + ~ConnectionPool(); + + void description(); + +protected: + ConnectionPool(); // 构造函数 +private: + // 不再需要解析配置文件的函数 + // bool parseJsonFile(); // 解析配置 + + void produce(); + void recycle(); + void addConnection(); // 增加连接数 + + // 写死的数据库连接参数 + string m_ip = "127.0.0.1"; + string m_userName = "root"; + string m_passwd = "123456"; + string m_db = "mayi_kunlun"; + unsigned short m_port = 3306; + + int m_max_conn; + int m_min_conn; + + int m_timeout; // 连接超时时间 + int max_del_time; // 最大删除时间( 连接空闲时间超过这个,就给当前连接关闭 ) + queue<MysqlConn*> m_connkQueue; // 连接队列 + mutex m_mutex; // 互斥锁 + condition_variable m_cond; // 条件变量 }; -- Gitblit v1.8.0