From cec6937344e179a6db50c6386b24afcff165561f Mon Sep 17 00:00:00 2001
From: zyf <2786722087@qq.com>
Date: 星期三, 02 四月 2025 09:21:04 +0800
Subject: [PATCH] zyf_0402_log
---
/dev/null | 57 ---------------------------------------------------------
Server/张怡帆/log/~WRL1218.tmp | 0
Server/张怡帆/log/日志_张怡帆_250402.doc | 0
3 files changed, 0 insertions(+), 57 deletions(-)
diff --git "a/Server/\345\274\240\346\200\241\345\270\206/code/ConnectionPool.cpp" "b/Server/\345\274\240\346\200\241\345\270\206/code/ConnectionPool.cpp"
deleted file mode 100644
index 92964d3..0000000
--- "a/Server/\345\274\240\346\200\241\345\270\206/code/ConnectionPool.cpp"
+++ /dev/null
@@ -1,133 +0,0 @@
-#include "ConnectionPool.h"
-
-ConnectionPool::ConnectionPool()
-{
- if (!parseConfig()) {
- cerr << "解析失败!" << endl;
- return;
- }
- for (m_num = 0; m_num < m_minSize;) {
- bool flag = addConnection();
- if (!flag) {
- return;
- }
- }
- thread producerThread(&ConnectionPool::producer, this);
- thread recyclerThread(&ConnectionPool::recycler, this);
- producerThread.detach();
- recyclerThread.detach();
-}
-
-ConnectionPool::~ConnectionPool()
-{
- while (!m_connections.empty()) {
- ConnectMysql* conn = m_connections.front();
- m_connections.pop();
- delete conn;
- cout << "连接已断开" << endl;
- }
-}
-
-//解析配置文件函数实现
-bool ConnectionPool::parseConfig()
-{
- m_ip = "192.168.133.129";
- m_port = 3306;
- m_user = "root"; // 替换为实际的数据库用户名
- m_passwd = "1"; // 替换为实际的数据库密码
- m_dbName = "AI_datas"; // 替换为实际的数据库名
- m_minSize = 1;
- m_maxSize = 20;
- m_timeout = 1000;
- m_maxIdleTime = 60000;
- return true;
-}
-
-// 添加数据库连接到连接池的函数实现
-bool ConnectionPool::addConnection()
-{
- ConnectMysql* conn = new ConnectMysql;
- bool res = conn->connect(m_user, m_passwd, m_dbName, m_ip, m_port);
- if (res) {
- conn->refreashAliveTime();//设置连接存活时间
- m_connections.push(conn);//将连接添加到连接池中
- ++m_num;
- cout << "连接成功!" << endl;
- return true;
- }
- else {
- delete conn;
- cerr << "连接失败!" << endl;
- return false;
- }
-}
-
-//创建新连接函数实现
-void ConnectionPool::producer()
-{
- while (true) {
- unique_lock<mutex> lock(m_mutex);
- m_cond.wait(lock, [this] { return m_connections.empty() && m_num < m_maxSize; });//线程等待,直到连接池为空或者连接数小于最大连接数
- if (m_num < m_maxSize) {
- bool flag = addConnection();
- if (!flag) {
- cerr << "创建新连接失败!" << endl;
- return;
- }
- }
- m_cond1.notify_all();
- }
-}
-
-//回收空闲连接函数实现
-void ConnectionPool::recycler()
-{
- while (true) {
- this_thread::sleep_for(chrono::milliseconds(500));//休眠0.5秒
- lock_guard<mutex> lock(m_mutex);
- while (!m_connections.empty() && m_num > m_minSize) {
- ConnectMysql* conn = m_connections.front();
- if (conn->getAliveTime() >= m_maxIdleTime) {
- m_connections.pop();
- delete conn;
- --m_num;
- cout << "回收空闲连接" << endl;
- }
- else {
- break;
- }
- }
- }
-}
-
-//获取连接池对象函数实现
-ConnectionPool* ConnectionPool::getInstance()
-{
- // 不使用互斥锁的线程安全的懒汉模式
- static ConnectionPool pool;// 静态局部变量,只初始化一次
- return &pool;
-}
-
-//获取连接函数实现
-shared_ptr<ConnectMysql> ConnectionPool::getConnection()
-{
- unique_lock<mutex> lock(m_mutex);
- while (m_connections.empty()) {
- if (cv_status::timeout == m_cond1.wait_for(lock, chrono::milliseconds(m_timeout))) { // 是否因为超时而退出等待
- if (m_connections.empty()) { // 超时且连接池仍为空,返回空指针
- cerr << "获取连接超时,连接池为空!" << endl;
- return nullptr;
- }
- }
- }
- shared_ptr<ConnectMysql> conn(m_connections.front(), [this](ConnectMysql* conn) {
- unique_lock<mutex> lc(m_mutex);
- conn->refreashAliveTime();
- m_connections.push(conn);
- cout << "连接已归还到连接池!" << endl;
- });
- m_connections.pop();
- m_cond.notify_all();
- cout << "从连接池获取连接成功!" << endl;
- return conn;
-}
\ No newline at end of file
diff --git "a/Server/\345\274\240\346\200\241\345\270\206/code/ConnectionPool.h" "b/Server/\345\274\240\346\200\241\345\270\206/code/ConnectionPool.h"
deleted file mode 100644
index 7538afe..0000000
--- "a/Server/\345\274\240\346\200\241\345\270\206/code/ConnectionPool.h"
+++ /dev/null
@@ -1,57 +0,0 @@
-#pragma once
-#include "ConnectMysql.h"
-#include <queue>
-#include <mutex>
-#include <thread>
-#include <atomic>
-#include <condition_variable>
-
-using namespace std;
-
-/*概念:数据库连接池是程序启动时建立足够的数据库连接,并将这些连接组成一个连接池,由程序动态地对池中的连接进行申请,使用,释放。
-作用:
-1.资源重用,避免了数据库连接频繁建立、关闭的开销
-2.更快的系统响应速度,直接从连接池中获取连接,响应速度加快
-3.控制资源的使用。如果不使用连接池,每次访问数据库都需要创建一个连接,这样系统的稳定性受系统连接需求影响很大,很容易产生资源浪费和高负载异常。连接池能够使性能最大化,将资源利用控制在一定的水平之下。连接池能控制池中的连接数量,增强了系统在大量用户应用时的稳定性。
-*/
-
-class ConnectionPool// 连接池类, 单例模式--懒汉
-{
-private:
- ConnectionPool();
- ConnectionPool(const ConnectionPool&) = delete;
- ConnectionPool& operator=(const ConnectionPool&) = delete;
- ~ConnectionPool();
-
- bool parseConfig(); // 解析配置文件
-
- bool addConnection(); // 添加新的数据库连接到连接池
-
- void producer(); // 生产者线程函数,负责创建新连接
-
- void recycler(); // 回收者线程函数,负责回收空闲连接
-
- queue<ConnectMysql*> m_connections; // 存放数据库连接池建立的连接
-
- string m_ip = "192.168.133.129";// IP
- unsigned short m_port = 3306;// 端口
- string m_user = "root";// 用户名
- string m_passwd = "1";// 密码
- string m_dbName = "AI_datas";// 数据库名
- 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* getInstance(); // 获取连接池单例对象
-
- shared_ptr<ConnectMysql> getConnection(); // 从连接池获取一个数据库连接
-};
diff --git "a/Server/\345\274\240\346\200\241\345\270\206/log/~WRL1218.tmp" "b/Server/\345\274\240\346\200\241\345\270\206/log/~WRL1218.tmp"
new file mode 100644
index 0000000..870dc7a
--- /dev/null
+++ "b/Server/\345\274\240\346\200\241\345\270\206/log/~WRL1218.tmp"
Binary files differ
diff --git "a/Server/\345\274\240\346\200\241\345\270\206/log/\346\227\245\345\277\227_\345\274\240\346\200\241\345\270\206_250402.doc" "b/Server/\345\274\240\346\200\241\345\270\206/log/\346\227\245\345\277\227_\345\274\240\346\200\241\345\270\206_250402.doc"
new file mode 100644
index 0000000..8fb7d75
--- /dev/null
+++ "b/Server/\345\274\240\346\200\241\345\270\206/log/\346\227\245\345\277\227_\345\274\240\346\200\241\345\270\206_250402.doc"
Binary files differ
--
Gitblit v1.8.0