数据库MySql类库系列(九)-DBServicePool
2017-03-09 14:29
316 查看
第七个工具类:DBServicePool
是针对前文提到的DBService的一个管理器
用于多线程环境下,可能有需求需要创建多个DBService,使得每个线程有机会可以独享一个DBService
所以有了这样一个DBService的管理器
提供Start,Stop,用来运行/停止所有DBService对象
提供Ping,用来维持所有DBService对象的心跳
提供一个虚接口Service,子类可以实现管理自己的继承自DBService的子类对象
比如:
实现一个MyDBService : public DBService
实现一个MyDBServicePool : public DBServicePool
DBServicePool的Service接口,实现的是创建一个DBService对象,返回其shared_ptr
MyDBServicePool 的Service接口,可以自行实现创建一个MyDBService对象,返回其shared_ptr
2017.3.10修改:
改为模板实现
模板类型T,T要求为DBService或其子类
Start并入构造函数,构造时创建指定个数的T对象,可以指定为0个,即不创建任何DBService或其子类对象
虚接口Service删除,不再需要,创建时,直接new T(...)
GetDBService:获取一个DBService对象,当Pool中没有DBService对象时(构造时指定了个数为0),抛一个异常
修改后代码:
DBServicePool.h
#ifndef __DBServicePool_H__
#define __DBServicePool_H__
#include <boost/noncopyable.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include "DBService.h"
namespace common{
namespace db{
template <class T>
class DBServicePool : private boost::noncopyable
{
public:
DBServicePool(unsigned int size,
const std::string& hostname,
unsigned int port,
const std::string& username,
const std::string& userkey,
const std::string& dbname);
virtual ~DBServicePool();
// 停止所有DBService对象
void Stop();
DBService& GetDBService();
// 获取IOService对象个数
std::size_t Size();
// 维持链接心跳
void Ping();
private:
boost::mutex m_Lock;
// DBService列表
std::vector<boost::shared_ptr<DBService> > m_DBServices;
// 顺序使用的下一个DBService下标
unsigned int m_Next;
};
template <class T>
DBServicePool<T>::DBServicePool(unsigned int size,
const std::string& hostname,
unsigned int port,
const std::string& username,
const std::string& userkey,
const std::string& dbname)
{
for (unsigned int i = 0; i < size; i++)
{
boost::shared_ptr<DBService> dbService(new T());
if (NULL != dbService)
{
dbService->Start(hostname, port, username, userkey, dbname);
m_DBServices.push_back(dbService);
}
}
m_Next = 0;
}
template <class T>
DBServicePool<T>::~DBServicePool()
{
}
template <class T>
void DBServicePool<T>::Stop()
{
boost::mutex::scoped_lock lock(m_Lock);
for (std::size_t i = 0; i < m_DBServices.size(); ++i)
{
m_DBServices[i]->Stop();
}
}
template <class T>
DBService& DBServicePool<T>::GetDBService()
{
boost::mutex::scoped_lock lock(m_Lock);
if (0 < m_DBServices.size())
{
m_Next++;
if (m_Next >= m_DBServices.size())
{
m_Next = 0;
}
return *m_DBServices[m_Next];
}
else
{
throw std::string("DBServicePool size is 0");
}
}
template <class T>
std::size_t DBServicePool<T>::Size()
{
boost::mutex::scoped_lock lock(m_Lock);
return m_DBServices.size();
}
template <class T>
void DBServicePool<T>::Ping()
{
boost::mutex::scoped_lock lock(m_Lock);
for (std::size_t i = 0; i < m_DBServices.size(); ++i)
{
m_DBServices[i]->Ping();
}
}
}
}
#endif
是针对前文提到的DBService的一个管理器
用于多线程环境下,可能有需求需要创建多个DBService,使得每个线程有机会可以独享一个DBService
所以有了这样一个DBService的管理器
提供Start,Stop,用来运行/停止所有DBService对象
提供Ping,用来维持所有DBService对象的心跳
提供一个虚接口Service,子类可以实现管理自己的继承自DBService的子类对象
比如:
实现一个MyDBService : public DBService
实现一个MyDBServicePool : public DBServicePool
DBServicePool的Service接口,实现的是创建一个DBService对象,返回其shared_ptr
MyDBServicePool 的Service接口,可以自行实现创建一个MyDBService对象,返回其shared_ptr
2017.3.10修改:
改为模板实现
模板类型T,T要求为DBService或其子类
Start并入构造函数,构造时创建指定个数的T对象,可以指定为0个,即不创建任何DBService或其子类对象
虚接口Service删除,不再需要,创建时,直接new T(...)
GetDBService:获取一个DBService对象,当Pool中没有DBService对象时(构造时指定了个数为0),抛一个异常
修改后代码:
DBServicePool.h
#ifndef __DBServicePool_H__
#define __DBServicePool_H__
#include <boost/noncopyable.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include "DBService.h"
namespace common{
namespace db{
template <class T>
class DBServicePool : private boost::noncopyable
{
public:
DBServicePool(unsigned int size,
const std::string& hostname,
unsigned int port,
const std::string& username,
const std::string& userkey,
const std::string& dbname);
virtual ~DBServicePool();
// 停止所有DBService对象
void Stop();
DBService& GetDBService();
// 获取IOService对象个数
std::size_t Size();
// 维持链接心跳
void Ping();
private:
boost::mutex m_Lock;
// DBService列表
std::vector<boost::shared_ptr<DBService> > m_DBServices;
// 顺序使用的下一个DBService下标
unsigned int m_Next;
};
template <class T>
DBServicePool<T>::DBServicePool(unsigned int size,
const std::string& hostname,
unsigned int port,
const std::string& username,
const std::string& userkey,
const std::string& dbname)
{
for (unsigned int i = 0; i < size; i++)
{
boost::shared_ptr<DBService> dbService(new T());
if (NULL != dbService)
{
dbService->Start(hostname, port, username, userkey, dbname);
m_DBServices.push_back(dbService);
}
}
m_Next = 0;
}
template <class T>
DBServicePool<T>::~DBServicePool()
{
}
template <class T>
void DBServicePool<T>::Stop()
{
boost::mutex::scoped_lock lock(m_Lock);
for (std::size_t i = 0; i < m_DBServices.size(); ++i)
{
m_DBServices[i]->Stop();
}
}
template <class T>
DBService& DBServicePool<T>::GetDBService()
{
boost::mutex::scoped_lock lock(m_Lock);
if (0 < m_DBServices.size())
{
m_Next++;
if (m_Next >= m_DBServices.size())
{
m_Next = 0;
}
return *m_DBServices[m_Next];
}
else
{
throw std::string("DBServicePool size is 0");
}
}
template <class T>
std::size_t DBServicePool<T>::Size()
{
boost::mutex::scoped_lock lock(m_Lock);
return m_DBServices.size();
}
template <class T>
void DBServicePool<T>::Ping()
{
boost::mutex::scoped_lock lock(m_Lock);
for (std::size_t i = 0; i < m_DBServices.size(); ++i)
{
m_DBServices[i]->Ping();
}
}
}
}
#endif
相关文章推荐
- 数据库MySql类库系列(二)-DBService
- 数据库MySql类库系列(三)-QueryOperatorUpdate
- 数据库MySql类库系列(一)-DBOperator
- 数据库MySql类库系列(六)-PrepareOperatorUpdate
- 数据库MySql类库系列(八)-预处理执行Sql方式的示例
- 数据库MySql类库系列(七)-PrepareOperatorSelect
- MySql 数据库系列问题
- mysql系列:数据库优化
- 卦卦学mysql系列(1)——mysql入门 对数据库的操作
- 渣渣小本求职复习之路每天一博客系列——数据库基础(MySQL)(1)
- 后端分布式系列:分布式存储-MySQL 数据库事务与复制
- JBoss 系列七十八: jBPM 6 使用 Mysql 替换默认的 H2 数据库
- mysql系列:ddl数据库定义+sql操作语句
- MySQL 数据库性能优化之SQL优化(这是 MySQL数据库性能优化专题 系列的第四篇文章)
- MySQL系列:查看并修改当前数据库的编码
- PowerDesigner系列之四:生成数据库时的列中文注释乱码问题[mysql]
- 【十分钟系列课程】第二集~MySQL客户端安装及创建数据库
- MySQL 数据库性能优化之索引优化(这是 MySQL数据库性能优化专题 系列的第三篇文章)
- Delphi XE3 数据库系列(一):使用dbexpress访问mysql
- 渣渣小本求职复习之路每天一博客系列——数据库基础(MySQL)(2)