久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

基于MySQL Connector/C++的數(shù)據(jù)庫連接池

 quasiceo 2016-06-23

基于MySQL Connector/C++的數(shù)據(jù)庫連接池

開源中國

開源中國

發(fā)表于 2014-08-20 23:02:04

之前發(fā)了基于MySQL c語言接口(libmysqlclinet)實現(xiàn) 
linux下c++寫的MySQL操作類
 


MySQL提供Connector c++(libmysqlcppconn)連接驅動.





具體參考:
基于MysqlConnector/C++的數(shù)據(jù)庫連接池的實現(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

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,,不代表本站觀點,。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,,謹防詐騙,。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報,。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多