zyf
2025-03-20 6cc7dc16fe70f56fe3094b9f4c8858f3c3b0429d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#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;
}