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
00095 virtual Connection* grab();
00096
00109 virtual void release(const Connection* pc);
00110
00112 void shrink() { clear(false); }
00113
00114 protected:
00123 void clear(bool all = true);
00124
00135 virtual Connection* create() = 0;
00136
00144 virtual void destroy(Connection*) = 0;
00145
00154 virtual unsigned int max_idle_time() = 0;
00155
00157 size_t size() const { return pool_.size(); }
00158
00159 private:
00161 struct ConnectionInfo {
00162 Connection* conn;
00163 time_t last_used;
00164 bool in_use;
00165
00166 ConnectionInfo(Connection* c) :
00167 conn(c),
00168 last_used(time(0)),
00169 in_use(true)
00170 {
00171 }
00172
00173
00174
00175
00176
00177
00178 bool operator<(const ConnectionInfo& rhs) const
00179 {
00180 const ConnectionInfo& lhs = *this;
00181 return lhs.in_use == rhs.in_use ?
00182 lhs.last_used < rhs.last_used :
00183 lhs.in_use;
00184 }
00185 };
00186 typedef std::list<ConnectionInfo> PoolT;
00187 typedef PoolT::iterator PoolIt;
00188
00190 Connection* find_mru();
00191 void remove_old_connections();
00192
00194 PoolT pool_;
00195 BeecryptMutex mutex_;
00196 };
00197
00198 }
00199
00200 #endif // !defined(MYSQLPP_CPOOL_H)
00201