您的位置:首页 > 编程语言 > C语言/C++

基于MySQL Connector/C++的数据库连接池

2016-05-17 22:41 651 查看
本文来自:http://www.oschina.net/code/snippet_583625_19818
/**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; //当前已建立的数据库连接数量int maxSize; //连接池中定义的最大数据库连接数string username;string password;string url;list<Connection*> connList; //连接池的容器队列pthread_mutex_t lock; //线程锁static ConnPool *connPool;Driver*driver;Connection*CreateConnection(); //创建一个连接void InitConnection(int iInitialSize); //初始化数据库连接池void DestoryConnection(Connection *conn); //销毁数据库连接对象void DestoryConnPool(); //销毁数据库连接池ConnPool(string url, string user, string password, int maxSize); //构造方法public:~ConnPool();Connection*GetConnection(); //获得数据库连接void ReleaseConnection(Connection *conn); //将数据库连接放回到连接池的容器中static ConnPool *GetInstance(); //获取数据库连接池对象};#endif  /*_CONNECTION_POOL_H */
/** 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;//连接池的构造函数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;}//初始化连接池,创建最大连接数的一半连接数量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("创建CONNECTION出错");}}pthread_mutex_unlock(&lock);}//创建连接,返回一个ConnectionConnection* ConnPool::CreateConnection() {Connection*conn;try {conn = driver->connect(this->url, this->username, this->password); //建立连接return conn;} catch (sql::SQLException&e) {perror("创建连接出错");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()) { //如果连接已经被关闭,删除后重新建立一个delete con;con = this->CreateConnection();}//如果连接为空,则创建连接出错if (con == NULL) {--curSize;}pthread_mutex_unlock(&lock);return con;} else {if (curSize < maxSize) { //还可以创建新的连接con = this->CreateConnection();if (con) {++curSize;pthread_mutex_unlock(&lock);return con;} else {pthread_mutex_unlock(&lock);return NULL;}} else { //建立的连接数已经达到maxSizepthread_mutex_unlock(&lock);return NULL;}}}//回收数据库连接void ConnPool::ReleaseConnection(sql::Connection * conn) {if (conn) {pthread_mutex_lock(&lock);connList.push_back(conn);pthread_mutex_unlock(&lock);}}//连接池的析构函数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;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: