//关闭连接池中所有的连接,并清空连接池 void MySQLConnectionPool::closeConnectionPool() { // 确保连接池存在,如果不存在,返回 pthread_mutex_lock(&mutex_connectionpool); if (connections.empty()) { pthread_mutex_unlock(&mutex_connectionpool); return; } PooledConnection *pConn = NULL; for(int i = 0 ; i < connections.size(); i++){ pConn = connections.at(i); // 如果忙,等 5 秒 if (pConn->isBusy()) { wait(5000); // 等 5 秒 } //5 秒后直接关闭它 closeConnection(pConn->getConnection()); // 从连接池向量中删除它 delete pConn; connections.erase(connections.begin()+i); } pthread_mutex_unlock(&mutex_connectionpool); // 置连接池为空 connections.clear(); }
//刷新连接池中所有的连接对象 void MySQLConnectionPool::refreshConnections() { // 确保连接池己创新存在 pthread_mutex_lock(&mutex_connectionpool); if (connections.empty()) { pthread_mutex_unlock(&mutex_connectionpool); return; } PooledConnection *pConn = NULL; for(int i =0 ; i<connections.size(); i++) { // 获得一个连接对象 pConn = connections.at(i); // 如果对象忙则等 5 秒 ,5 秒后直接刷新 if (pConn->isBusy()) { wait(5000); // 等 5 秒 } // 关闭此连接,用一个新的连接代替它。 closeConnection(pConn->getConnection()); pConn->setConnection(newConnection()); pConn->setBusy(false); } pthread_mutex_unlock(&mutex_connectionpool); }
void BloomfilterConnectionPool::recycleConnectionPool() { pthread_mutex_lock(&mutex_connectionpool); if (connections.empty()) { pthread_mutex_unlock(&mutex_connectionpool); return; } PooledConnection* pConn = NULL; for (int i = 0; i < connections.size(); i++) { pConn = connections.at(i); if (!pConn->isBusy()) { delete pConn->getConnection(); delete pConn; } else { delete pConn; } } connections.clear(); pthread_mutex_unlock(&mutex_connectionpool); return; }
ServClient* BloomfilterConnectionPool::findFreeConnection() { ServClient * conn = NULL; PooledConnection *pConn = NULL; for(int i = 0 ;i<connections.size(); i++) { pConn = connections.at(i); if (!pConn->isBusy()) { conn = pConn->getConnection(); pConn->setBusy(true); break; } } return conn; }
//查找连接池中所有的连接,查找一个可用的数据库连接 sql::Connection * MySQLConnectionPool::findFreeConnection() { sql::Connection * conn = NULL; PooledConnection *pConn = NULL; // 获得连接池向量中所有的对象 for(int i = 0 ;i<connections.size(); i++) { pConn = connections.at(i); if (!pConn->isBusy()) { // 如果此对象不忙,则获得它的数据库连接并把它设为忙 conn = pConn->getConnection(); // 测试此连接是否可用 if (!testConnection(conn)) { // 如果此连接不可再用了,则创建一个新的连接, // 并替换此不可用的连接对象,如果创建失败,返回 null try { conn = newConnection(); } catch(...) { return NULL; } if (conn) { pConn->setConnection(conn); pConn->setBusy(true); } else { return NULL; } } else { pConn->setBusy(true); } break; // 己经找到一个可用的连接,退出 } } //mysql_query( mysql, "SET NAMES" gbk"" ); return conn;// 返回找到到的可用连接 }
void MySQLConnectionPool::Destroy() { pthread_mutex_lock(&mutex_connectionpool); if (connections.empty()) { pthread_mutex_unlock(&mutex_connectionpool); return; } PooledConnection *pConn = NULL; // 遍历连接池中的所有连接,找到这个要返回的连接对象 for (int i= 0 ; i < connections.size() ; i++) { pConn =connections.at(i); closeConnection(pConn->getConnection()); } connections.clear(); pthread_mutex_unlock(&mutex_connectionpool); }
void BloomfilterConnectionPool::returnConnection(ServClient * conn) { pthread_mutex_lock(&mutex_connectionpool); if (connections.empty()) { pthread_mutex_unlock(&mutex_connectionpool); return; } PooledConnection* pConn = NULL; for (int i = 0; i < connections.size(); i++) { pConn = connections.at(i); if (conn == pConn->getConnection()) { pConn->setBusy(false); break; } } pthread_mutex_unlock(&mutex_connectionpool); }
//此函数返回一个数据库连接到连接池中,并把此连接置为空闲。 void MySQLConnectionPool::returnConnection(sql::Connection *conn) { pthread_mutex_lock(&mutex_connectionpool); if (connections.empty()) { pthread_mutex_unlock(&mutex_connectionpool); return; } PooledConnection *pConn = NULL; // 遍历连接池中的所有连接,找到这个要返回的连接对象 for (int i= 0 ; i < connections.size() ; i++) { pConn =connections.at(i); // 先找到连接池中的要返回的连接对象 if (conn == pConn->getConnection()) { // 找到了 , 设置此连接为空闲状态 pConn->setBusy(false); break; } } pthread_mutex_unlock(&mutex_connectionpool); }
void BloomfilterConnectionPool::closeConnectionPool() { #if 0 pthread_mutex_lock(&mutex_closepool); if (isClosing == 1) { while (isClosing == 1) { pthread_mutex_unlock(&mutex_closepool); //sleep(5); pthread_mutex_lock(&mutex_closepool); } pthread_mutex_unlock(&mutex_closepool); return; } isClosing = 1; pthread_mutex_unlock(&mutex_closepool); #endif pthread_mutex_lock(&mutex_connectionpool); if (connections.empty()) { pthread_mutex_unlock(&mutex_connectionpool); #if 0 pthread_mutex_lock(&mutex_closepool); isClosing = 0; pthread_mutex_unlock(&mutex_closepool); #endif return; } PooledConnection* pConn = NULL; int ret = connections.size(); for (int i = 0; i < connections.size(); i++) { pConn = connections.at(i); #if 0 while (pConn->isBusy()) { // 等50秒时间有点长,是为了防止dump时,阻塞了client的请求。找时间优化一下对这个地方的处理。 //wait(5); } #endif //delete pConn->getConnection(); if (!pConn->isBusy()) { delete pConn->getConnection(); delete pConn; } else { delete pConn; } // connections.erase(connections.begin()+i); } connections.clear(); pthread_mutex_unlock(&mutex_connectionpool); //printf("close executed unlock\n"); #if 0 pthread_mutex_lock(&mutex_closepool); isClosing = 0; pthread_mutex_unlock(&mutex_closepool); #endif }