conn_manager.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_CONN_MANAGER_H
  19. #define CPPDB_CONN_MANAGER_H
  20. #include <cppdb/defs.h>
  21. #include <cppdb/ref_ptr.h>
  22. #include <mutex>
  23. #include <map>
  24. #include <string>
  25. #include <memory>
  26. namespace cppdb {
  27. class pool;
  28. class connection_info;
  29. namespace backend {
  30. class connection;
  31. }
  32. ///
  33. /// \brief This class is the major gateway to new connections
  34. ///
  35. /// It handles connection pools and forwards request to the drivers.
  36. ///
  37. /// This class member functions are thread safe
  38. ///
  39. class CPPDB_API connections_manager {
  40. connections_manager();
  41. // Borland erros on hidden destructors in classes without only static methods.
  42. #ifndef __BORLANDC__
  43. ~connections_manager();
  44. #endif
  45. connections_manager(connections_manager const &);
  46. void operator = (connections_manager const &);
  47. public:
  48. ///
  49. /// Get a singleton instance of the class
  50. ///
  51. static connections_manager &instance();
  52. ///
  53. /// Create a new connection using connection string \a cs
  54. ///
  55. ref_ptr<backend::connection> open(std::string const &cs);
  56. ///
  57. /// Create a new connection using parsed connection string \a ci
  58. ///
  59. ref_ptr<backend::connection> open(connection_info const &ci);
  60. ///
  61. /// Collect all connections that were not used for long time and close them.
  62. ///
  63. void gc();
  64. private:
  65. struct data;
  66. std::unique_ptr<data> d;
  67. std::mutex lock_;
  68. typedef std::map<std::string,ref_ptr<pool> > connections_type;
  69. connections_type connections_;
  70. };
  71. } // cppdb
  72. #endif