当前位置:   article > 正文

数据库MySql类库系列(九)-DBServicePool_dbservice和mysql

dbservice和mysql

第七个工具类: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

  1. #ifndef __DBServicePool_H__
  2. #define __DBServicePool_H__
  3. #include <boost/noncopyable.hpp>
  4. #include <boost/thread.hpp>
  5. #include <boost/shared_ptr.hpp>
  6. #include "DBService.h"
  7. namespace common{
  8. namespace db{
  9. template <class T>
  10. class DBServicePool : private boost::noncopyable
  11. {
  12. public:
  13. DBServicePool(unsigned int size,
  14. const std::string& hostname,
  15. unsigned int port,
  16. const std::string& username,
  17. const std::string& userkey,
  18. const std::string& dbname);
  19. virtual ~DBServicePool();
  20. // 停止所有DBService对象
  21. void Stop();
  22. DBService& GetDBService();
  23. // 获取IOService对象个数
  24. std::size_t Size();
  25. // 维持链接心跳
  26. void Ping();
  27. private:
  28. boost::mutex m_Lock;
  29. // DBService列表
  30. std::vector<boost::shared_ptr<DBService> > m_DBServices;
  31. // 顺序使用的下一个DBService下标
  32. unsigned int m_Next;
  33. };
  34. template <class T>
  35. DBServicePool<T>::DBServicePool(unsigned int size,
  36. const std::string& hostname,
  37. unsigned int port,
  38. const std::string& username,
  39. const std::string& userkey,
  40. const std::string& dbname)
  41. {
  42. for (unsigned int i = 0; i < size; i++)
  43. {
  44. boost::shared_ptr<DBService> dbService(new T());
  45. if (NULL != dbService)
  46. {
  47. dbService->Start(hostname, port, username, userkey, dbname);
  48. m_DBServices.push_back(dbService);
  49. }
  50. }
  51. m_Next = 0;
  52. }
  53. template <class T>
  54. DBServicePool<T>::~DBServicePool()
  55. {
  56. }
  57. template <class T>
  58. void DBServicePool<T>::Stop()
  59. {
  60. boost::mutex::scoped_lock lock(m_Lock);
  61. for (std::size_t i = 0; i < m_DBServices.size(); ++i)
  62. {
  63. m_DBServices[i]->Stop();
  64. }
  65. }
  66. template <class T>
  67. DBService& DBServicePool<T>::GetDBService()
  68. {
  69. boost::mutex::scoped_lock lock(m_Lock);
  70. if (0 < m_DBServices.size())
  71. {
  72. m_Next++;
  73. if (m_Next >= m_DBServices.size())
  74. {
  75. m_Next = 0;
  76. }
  77. return *m_DBServices[m_Next];
  78. }
  79. else
  80. {
  81. throw std::string("DBServicePool size is 0");
  82. }
  83. }
  84. template <class T>
  85. std::size_t DBServicePool<T>::Size()
  86. {
  87. boost::mutex::scoped_lock lock(m_Lock);
  88. return m_DBServices.size();
  89. }
  90. template <class T>
  91. void DBServicePool<T>::Ping()
  92. {
  93. boost::mutex::scoped_lock lock(m_Lock);
  94. for (std::size_t i = 0; i < m_DBServices.size(); ++i)
  95. {
  96. m_DBServices[i]->Ping();
  97. }
  98. }
  99. }
  100. }
  101. #endif


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/992611
推荐阅读
相关标签
  

闽ICP备14008679号