From e654f4b7e6b45b20f0ee969161d461704cc41894 Mon Sep 17 00:00:00 2001 From: jhc <2991879965@qq.com> Date: 星期二, 05 十一月 2024 15:21:45 +0800 Subject: [PATCH] 上传日志 --- Server/王琨元/code/ConnectionPool.h | 82 +++++++++++++++++++--------------------- 1 files changed, 39 insertions(+), 43 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..bce6a07 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,50 @@ #pragma once -#include "MysqlConn.h" -#include "./tinyxml/tinyxml.h" + #include <queue> #include <mutex> -#include <thread> -#include <atomic> #include <condition_variable> +//#include <json/json.h> +#include "MysqlConn.h" -#pragma comment(lib, "./tinyxml/x64/Debug/tinyxml.lib") +//using namespace Json; -// 应用-单例模式:懒汉模式[需要考虑多线程安全问题] 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( ); + + std::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(); // 增加连接数 + + std::string m_ip; + std::string m_userName; + std::string m_passwd; + std::string m_db; + unsigned short m_port; + + int m_max_conn; + int m_min_conn; + + int m_timeout; // 连接超时时间 + int max_del_time; // 最大删除时间( 连接空闲时间超过这个,就给当前连接关闭 ) + std::queue<MysqlConn*>m_connkQueue ; // 连接队列 + std::mutex m_mutex; // 互斥锁 + std::condition_variable m_cond; // 条件变量 }; + -- Gitblit v1.8.0