基于MySQL Connector/C++的數(shù)據(jù)庫連接池開源中國發(fā)表于 2014-08-20 23:02:04
之前發(fā)了基于MySQL c語言接口(libmysqlclinet)實現(xiàn) /* *File: connection_pool.h *Author: csc */ #ifndef _CONNECTION_POOL_H #define_CONNECTION_POOL_H #include <mysql_connection.h> #include <mysql_driver.h> #include <cppconn/exception.h> #include <cppconn/driver.h> #include <cppconn/connection.h> #include <cppconn/resultset.h> #include <cppconn/prepared_statement.h> #include <cppconn/statement.h> #include <pthread.h> #include <list> using namespace std; using namespace sql; class ConnPool { private: int curSize; //當前已建立的數(shù)據(jù)庫連接數(shù)量 int maxSize; //連接池中定義的最大數(shù)據(jù)庫連接數(shù) string username; string password; string url; list<Connection*> connList; //連接池的容器隊列 pthread_mutex_t lock; //線程鎖 static ConnPool *connPool; Driver*driver; Connection*CreateConnection(); //創(chuàng)建一個連接 void InitConnection(int iInitialSize); //初始化數(shù)據(jù)庫連接池 void DestoryConnection(Connection *conn); //銷毀數(shù)據(jù)庫連接對象 void DestoryConnPool(); //銷毀數(shù)據(jù)庫連接池 ConnPool(string url, string user, string password, int maxSize); //構造方法 public: ~ConnPool(); Connection*GetConnection(); //獲得數(shù)據(jù)庫連接 void ReleaseConnection(Connection *conn); //將數(shù)據(jù)庫連接放回到連接池的容器中 static ConnPool *GetInstance(); //獲取數(shù)據(jù)庫連接池對象 }; #endif /* * connection_pool.cpp * * Created on: 2013-3-29 * Author: csc */ #include <stdexcept> #include <exception> #include <stdio.h> #include"connection_pool.h" using namespace std; using namespace sql; ConnPool *ConnPool::connPool = NULL; //連接池的構造函數(shù) ConnPool::ConnPool(string url, string userName, string password, int maxSize) { this->maxSize = maxSize; this->curSize = 0; this->username = userName; this->password = password; this->url = url; try { this->driver = sql::mysql::get_driver_instance(); } catch (sql::SQLException&e) { perror("驅動連接出錯;n"); } catch (std::runtime_error&e) { perror("運行出錯了n"); } this->InitConnection(maxSize / 2); } //獲取連接池對象,單例模式 ConnPool*ConnPool::GetInstance() { if (connPool == NULL) { connPool = new ConnPool("tcp://127.0.0.1:3306","root","123456", 50); } return connPool; } //初始化連接池,,創(chuàng)建最大連接數(shù)的一半連接數(shù)量 void ConnPool::InitConnection(int iInitialSize) { Connection*conn; pthread_mutex_lock(&lock); for (int i = 0; i < iInitialSize; i++) { conn = this->CreateConnection(); if (conn) { connList.push_back(conn); ++(this->curSize); } else { perror("創(chuàng)建CONNECTION出錯"); } } pthread_mutex_unlock(&lock); } //創(chuàng)建連接,返回一個Connection Connection* ConnPool::CreateConnection() { Connection*conn; try { conn = driver->connect(this->url, this->username, this->password); //建立連接 return conn; } catch (sql::SQLException&e) { perror("創(chuàng)建連接出錯"); return NULL; } catch (std::runtime_error&e) { perror("運行時出錯"); return NULL; } } //在連接池中獲得一個連接 Connection*ConnPool::GetConnection() { Connection*con; pthread_mutex_lock(&lock); if (connList.size() > 0) {//連接池容器中還有連接 con = connList.front(); //得到第一個連接 connList.pop_front(); //移除第一個連接 if (con->isClosed()) {//如果連接已經(jīng)被關閉,刪除后重新建立一個 delete con; con = this->CreateConnection(); } //如果連接為空,,則創(chuàng)建連接出錯 if (con == NULL) { --curSize; } pthread_mutex_unlock(&lock); return con; } else { if (curSize < maxSize) { //還可以創(chuàng)建新的連接 con = this->CreateConnection(); if (con) { ++curSize; pthread_mutex_unlock(&lock); return con; } else { pthread_mutex_unlock(&lock); return NULL; } } else { //建立的連接數(shù)已經(jīng)達到maxSize pthread_mutex_unlock(&lock); return NULL; } } } //回收數(shù)據(jù)庫連接 void ConnPool::ReleaseConnection(sql::Connection * conn) { if (conn) { pthread_mutex_lock(&lock); connList.push_back(conn); pthread_mutex_unlock(&lock); } } //連接池的析構函數(shù) ConnPool::~ConnPool() { this->DestoryConnPool(); } //銷毀連接池,首先要先銷毀連接池的中連接 void ConnPool::DestoryConnPool() { list<Connection*>::iterator icon; pthread_mutex_lock(&lock); for (icon = connList.begin(); icon != connList.end(); ++icon) { this->DestoryConnection(*icon); //銷毀連接池中的連接 } curSize = 0; connList.clear(); //清空連接池中的連接 pthread_mutex_unlock(&lock); } //銷毀一個連接 void ConnPool::DestoryConnection(Connection* conn) { if (conn) { try { conn->close(); } catch (sql::SQLException&e) { perror(e.what()); } catch (std::exception&e) { perror(e.what()); } delete conn; } } /* * main.cpp * * Created on: 2013-3-26 * Author: holy */ #include"connection_pool.h" namespace ConnectMySQL { //初始化連接池 ConnPool *connpool = ConnPool::GetInstance(); void run() { Connection *con; Statement *state; ResultSet *result; // 從連接池中獲取mysql連接 con = connpool->GetConnection(); state = con->createStatement(); state->execute("use holy"); // 查詢 result = state->executeQuery("select * from student where id < 1002"); // 輸出查詢 while (result->next()) { int id = result->getInt("id"); string name = result->getString("name"); cout << id <<":"<< name << endl; } delete state; connpool->ReleaseConnection(con); } } int main(int argc, char* argv[]) { ConnectMySQL::run(); return 0; } http://www./topics/0/85/85829.html |
|