| 1 | // file : odb/mysql/connection-factory.hxx |
| 2 | // copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC |
| 3 | // license : GNU GPL v2; see accompanying LICENSE file |
| 4 | |
| 5 | #ifndef ODB_MYSQL_CONNECTION_FACTORY_HXX |
| 6 | #define ODB_MYSQL_CONNECTION_FACTORY_HXX |
| 7 | |
| 8 | #include <odb/pre.hxx> |
| 9 | |
| 10 | #include <vector> |
| 11 | #include <cstddef> // std::size_t |
| 12 | #include <cassert> |
| 13 | |
| 14 | #include <odb/mysql/version.hxx> |
| 15 | #include <odb/mysql/forward.hxx> |
| 16 | #include <odb/mysql/connection.hxx> |
| 17 | |
| 18 | #include <odb/details/mutex.hxx> |
| 19 | #include <odb/details/condition.hxx> |
| 20 | #include <odb/details/shared-ptr.hxx> |
| 21 | |
| 22 | #include <odb/mysql/details/export.hxx> |
| 23 | |
| 24 | namespace odb |
| 25 | { |
| 26 | namespace mysql |
| 27 | { |
| 28 | class LIBODB_MYSQL_EXPORT connection_factory |
| 29 | { |
| 30 | public: |
| 31 | virtual connection_ptr |
| 32 | connect () = 0; |
| 33 | |
| 34 | public: |
| 35 | typedef mysql::database database_type; |
| 36 | |
| 37 | virtual void |
| 38 | database (database_type&) = 0; |
| 39 | |
| 40 | virtual |
| 41 | ~connection_factory (); |
| 42 | }; |
| 43 | |
| 44 | class LIBODB_MYSQL_EXPORT new_connection_factory: public connection_factory |
| 45 | { |
| 46 | public: |
| 47 | new_connection_factory () |
| 48 | : db_ (0) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | virtual connection_ptr |
| 53 | connect (); |
| 54 | |
| 55 | virtual void |
| 56 | database (database_type&); |
| 57 | |
| 58 | private: |
| 59 | new_connection_factory (const new_connection_factory&); |
| 60 | new_connection_factory& operator= (const new_connection_factory&); |
| 61 | |
| 62 | private: |
| 63 | database_type* db_; |
| 64 | }; |
| 65 | |
| 66 | class LIBODB_MYSQL_EXPORT connection_pool_factory: |
| 67 | public connection_factory |
| 68 | { |
| 69 | public: |
| 70 | // The max_connections argument specifies the maximum number of |
| 71 | // concurrent connections this pool will maintain. If this value |
| 72 | // is 0 then the pool will create a new connection every time all |
| 73 | // of the existing connections are in use. |
| 74 | // |
| 75 | // The min_connections argument specifies the minimum number of |
| 76 | // connections that should be maintained by the pool. If the |
| 77 | // number of connections maintained by the pool exceeds this |
| 78 | // number and there are no active waiters for a new connection, |
| 79 | // then the pool will release the excess connections. If this |
| 80 | // value is 0 then the pool will maintain all the connections |
| 81 | // that were ever created. |
| 82 | // |
| 83 | // The ping argument specifies whether to ping the connection to |
| 84 | // make sure it is still alive before returning it to the caller. |
| 85 | // |
| 86 | connection_pool_factory (std::size_t max_connections = 0, |
| 87 | std::size_t min_connections = 0, |
| 88 | bool ping = true) |
| 89 | : max_ (max_connections), |
| 90 | min_ (min_connections), |
| 91 | ping_ (ping), |
| 92 | in_use_ (0), |
| 93 | waiters_ (0), |
| 94 | db_ (0), |
| 95 | cond_ (mutex_) |
| 96 | { |
| 97 | // max_connections == 0 means unlimited. |
| 98 | // |
| 99 | assert (max_connections == 0 || max_connections >= min_connections); |
| 100 | } |
| 101 | |
| 102 | virtual connection_ptr |
| 103 | connect (); |
| 104 | |
| 105 | virtual void |
| 106 | database (database_type&); |
| 107 | |
| 108 | virtual |
| 109 | ~connection_pool_factory (); |
| 110 | |
| 111 | private: |
| 112 | connection_pool_factory (const connection_pool_factory&); |
| 113 | connection_pool_factory& operator= (const connection_pool_factory&); |
| 114 | |
| 115 | protected: |
| 116 | class LIBODB_MYSQL_EXPORT pooled_connection: public connection |
| 117 | { |
| 118 | public: |
| 119 | pooled_connection (database_type&); |
| 120 | pooled_connection (database_type&, MYSQL*); |
| 121 | |
| 122 | private: |
| 123 | static bool |
| 124 | zero_counter (void*); |
| 125 | |
| 126 | private: |
| 127 | friend class connection_pool_factory; |
| 128 | |
| 129 | shared_base::refcount_callback callback_; |
| 130 | |
| 131 | // NULL pool value indicates that the connection is not in use. |
| 132 | // |
| 133 | connection_pool_factory* pool_; |
| 134 | }; |
| 135 | |
| 136 | friend class pooled_connection; |
| 137 | |
| 138 | typedef details::shared_ptr<pooled_connection> pooled_connection_ptr; |
| 139 | typedef std::vector<pooled_connection_ptr> connections; |
| 140 | |
| 141 | // This function is called whenever the pool needs to create a new |
| 142 | // connection. |
| 143 | // |
| 144 | virtual pooled_connection_ptr |
| 145 | create (); |
| 146 | |
| 147 | protected: |
| 148 | // Return true if the connection should be deleted, false otherwise. |
| 149 | // |
| 150 | bool |
| 151 | release (pooled_connection*); |
| 152 | |
| 153 | protected: |
| 154 | const std::size_t max_; |
| 155 | const std::size_t min_; |
| 156 | const bool ping_; |
| 157 | |
| 158 | std::size_t in_use_; // Number of connections currently in use. |
| 159 | std::size_t waiters_; // Number of threads waiting for a connection. |
| 160 | |
| 161 | database_type* db_; |
| 162 | connections connections_; |
| 163 | |
| 164 | details::mutex mutex_; |
| 165 | details::condition cond_; |
| 166 | }; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | #include <odb/post.hxx> |
| 171 | |
| 172 | #endif // ODB_MYSQL_CONNECTION_FACTORY_HXX |