00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #if !defined(MYSQLPP_CPOOL_H)
00029 #define MYSQLPP_CPOOL_H
00030
00031 #include "beemutex.h"
00032
00033 #include <list>
00034
00035 #include <assert.h>
00036 #include <time.h>
00037
00038 namespace mysqlpp {
00039
00040 #if !defined(DOXYGEN_IGNORE)
00041
00042 class MYSQLPP_EXPORT Connection;
00043 #endif
00044
00067
00068 class MYSQLPP_EXPORT ConnectionPool
00069 {
00070 public:
00072 ConnectionPool() { }
00073
00078 virtual ~ConnectionPool() { assert(empty()); }
00079
00081 bool empty() const { return pool_.empty(); }
00082
00106 virtual Connection* exchange(const Connection* pc);
00107
00120 virtual Connection* grab();
00121
00137 virtual void release(const Connection* pc);
00138
00150 void remove(const Connection* pc);
00151
00162 virtual Connection* safe_grab();
00163
00165 void shrink() { clear(false); }
00166
00167 protected:
00176 void clear(bool all = true);
00177
00188 virtual Connection* create() = 0;
00189
00197 virtual void destroy(Connection*) = 0;
00198
00207 virtual unsigned int max_idle_time() = 0;
00208
00210 size_t size() const { return pool_.size(); }
00211
00212 private:
00214 struct ConnectionInfo {
00215 Connection* conn;
00216 time_t last_used;
00217 bool in_use;
00218
00219 ConnectionInfo(Connection* c) :
00220 conn(c),
00221 last_used(time(0)),
00222 in_use(true)
00223 {
00224 }
00225
00226
00227
00228
00229
00230
00231 bool operator<(const ConnectionInfo& rhs) const
00232 {
00233 const ConnectionInfo& lhs = *this;
00234 return lhs.in_use == rhs.in_use ?
00235 lhs.last_used < rhs.last_used :
00236 lhs.in_use;
00237 }
00238 };
00239 typedef std::list<ConnectionInfo> PoolT;
00240 typedef PoolT::iterator PoolIt;
00241
00243 Connection* find_mru();
00244 void remove(const PoolIt& it);
00245 void remove_old_connections();
00246
00248 PoolT pool_;
00249 BeecryptMutex mutex_;
00250 };
00251
00252 }
00253
00254 #endif // !defined(MYSQLPP_CPOOL_H)
00255