#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;
|
}
|