Mysql Connector(C++)ベースのデータベース接続プールの実装

4879 ワード

データベース接続プールの主な操作をまとめることができます.
(1)まずデータベース接続プールオブジェクトを作成する
(2)一定数のデータベース接続を初期化し、接続プールオブジェクトのコンテナに入れる
(3)データベースアクセス要求がある場合、接続プールのコンテナから直接接続を取得します.
(a)コンテナに接続がある場合は、データベースアクセス要求者への接続を返す
(b)コンテナに接続がなく、現在確立されている接続数がシステム定義の最大接続数に達していない場合、新しいデータベース接続を作成する.
#include
#include
#include
#include"connection_pool.h"

usingnamespace std;
usingnamespace sql;

ConnPool*ConnPool::connPool=NULL;
//        
ConnPool::ConnPool(stringurl, 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("      ;
"); } catch(std::runtime_error&e) { perror("
"); } this->InitConnection(maxSize/2); } // , ConnPool*ConnPool::GetInstance(){ if(connPool==NULL) { connPool=newConnPool("tcp://127.0.0.1:3306","root","root",50); } returnconnPool; } // , voidConnPool::InitConnection(int iInitialSize) { Connection*conn; pthread_mutex_lock(&lock); for(inti=0;iCreateConnection(); if(conn){ connList.push_back(conn); ++(this->curSize); } else { perror(" CONNECTION "); } } pthread_mutex_unlock(&lock); } // , Connection Connection*ConnPool::CreateConnection(){ Connection*conn; try{ conn=driver->connect(this->url,this->username,this->password);// returnconn; } catch(sql::SQLException&e) { perror(" "); returnNULL; } catch(std::runtime_error&e) { perror(" "); returnNULL; } } // Connection*ConnPool::GetConnection(){ Connection*con; pthread_mutex_lock(&lock); if(connList.size()>0)// { con=connList.front();// connList.pop_front();// if(con->isClosed())// , { deletecon; con=this->CreateConnection(); } // , if(con==NULL) { --curSize; } pthread_mutex_unlock(&lock); returncon; } else{ if(curSize< maxSize){// con= this->CreateConnection(); if(con){ ++curSize; pthread_mutex_unlock(&lock); returncon; } else{ pthread_mutex_unlock(&lock); returnNULL; } } else{// maxSize pthread_mutex_unlock(&lock); returnNULL; } } } // voidConnPool::ReleaseConnection(sql::Connection * conn){ if(conn){ pthread_mutex_lock(&lock); connList.push_back(conn); pthread_mutex_unlock(&lock); } } // ConnPool::~ConnPool() { this->DestoryConnPool(); } // , voidConnPool::DestoryConnPool(){ list::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); } // voidConnPool::DestoryConnection(Connection* conn) { if(conn) { try{ conn->close(); } catch(sql::SQLException&e) { perror(e.what()); } catch(std::exception&e) { perror(e.what()); } deleteconn; } }

(c)コンテナ内の接続がなく、現在確立されている接続数がシステム定義の最大接続数に達すると、現在のアクセスデータベース要求は、他のアクセス要求が接続を解放するのを待つ.
(4)データベースアクセスが完了したら、接続を接続プールのコンテナに戻す.
(5)サービスが停止した場合、データベース接続プール内のすべてのデータベース接続を解放してから、データベース接続プールオブジェクトを解放する必要があります.
2.プログラミング実装:
ヘッダファイル(connection_pool.h):
 /*
         *File: connection_pool.h
          *Author: csc
     */
#ifndef_CONNECTION_POOL_H
#define	_CONNECTION_POOL_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
usingnamespace std;
usingnamespace sql;

classConnPool{
private:
intcurSize;//             
intmaxSize;//               
stringusername;
stringpassword;
stringurl;
listconnList;//        
pthread_mutex_tlock;//   
staticConnPool *connPool;
Driver*driver;

Connection*CreateConnection();//      
voidInitConnection(int iInitialSize);//         
voidDestoryConnection(Connection *conn);//         
voidDestoryConnPool();//        
ConnPool(stringurl,string user,string password,int maxSize);//    
public:
~ConnPool();
Connection*GetConnection();//       
voidReleaseConnection(Connection *conn);//                
staticConnPool *GetInstance();//          
};
#endif	/*_CONNECTION_POOL_H */

///////////////////////////////