pool.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // Distributed under:
  6. //
  7. // the Boost Software License, Version 1.0.
  8. // (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // or (at your opinion) under:
  12. //
  13. // The MIT License
  14. // (See accompanying file MIT.txt or a copy at
  15. // http://www.opensource.org/licenses/mit-license.php)
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #ifndef CPPDB_POOL_H
  19. #define CPPDB_POOL_H
  20. #include <cppdb/defs.h>
  21. #include <cppdb/ref_ptr.h>
  22. #include <mutex>
  23. #include <cppdb/utils.h>
  24. #include <memory>
  25. #include <list>
  26. namespace cppdb {
  27. class connection_info;
  28. namespace backend {
  29. class connection;
  30. }
  31. ///
  32. /// \brief Connections pool, allows to handle multiple connections for specific connection string.
  33. ///
  34. /// Note \ref connections_manager provide more generic interface and hides pools inside it. So you
  35. /// generally should use this class only when you prefer to avoid using some global singleton object.
  36. ///
  37. /// Unlike \ref connections_manager, it uses pool by default unless its size defined as 0.
  38. ///
  39. /// All this class member functions are thread safe to use from several threads for the same object
  40. ///
  41. class CPPDB_API pool : public ref_counted {
  42. pool();
  43. pool(pool const &);
  44. void operator=(pool const &);
  45. pool(connection_info const &ci);
  46. public:
  47. /// Create new pool for \a connection_string
  48. static ref_ptr<pool> create(std::string const &connection_string);
  49. /// Create new pool for a parsed connection string \a ci
  50. static ref_ptr<pool> create(connection_info const &ci);
  51. ///
  52. /// Shortcut of cppdb::ref_ptr<cppdb::pool> as cppdb::pool::pointer.
  53. ///
  54. /// The pointer that is used to handle pool object
  55. ///
  56. typedef ref_ptr<pool> pointer;
  57. ~pool();
  58. ///
  59. /// Get a open a connection, it may be fetched either from pool or new one may be created
  60. ///
  61. ref_ptr<backend::connection> open();
  62. ///
  63. /// Collect connections that were not used for a long time (close them)
  64. ///
  65. void gc();
  66. ///
  67. /// Remove all connections from the pool
  68. ///
  69. void clear();
  70. /// \cond INTERNAL
  71. void put(backend::connection *c_in);
  72. /// \endcond
  73. private:
  74. ref_ptr<backend::connection> get();
  75. struct data;
  76. std::unique_ptr<data> d;
  77. struct entry {
  78. entry() : last_used(0) {}
  79. ref_ptr<backend::connection> conn;
  80. std::time_t last_used;
  81. };
  82. typedef std::list<entry> pool_type;
  83. // non-mutable members
  84. size_t limit_;
  85. int life_time_;
  86. connection_info ci_;
  87. // mutex protected begin
  88. std::mutex lock_;
  89. size_t size_;
  90. pool_type pool_;
  91. // mutex protected end
  92. };
  93. }
  94. #endif