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 Connection* grab();
00096
00109 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
00156 private:
00158 struct ConnectionInfo {
00159 Connection* conn;
00160 time_t last_used;
00161 bool in_use;
00162
00163 ConnectionInfo(Connection* c) :
00164 conn(c),
00165 last_used(time(0)),
00166 in_use(true)
00167 {
00168 }
00169
00170
00171
00172
00173
00174
00175 bool operator<(const ConnectionInfo& rhs) const
00176 {
00177 const ConnectionInfo& lhs = *this;
00178 return lhs.in_use == rhs.in_use ?
00179 lhs.last_used < rhs.last_used :
00180 lhs.in_use;
00181 }
00182 };
00183 typedef std::list<ConnectionInfo> PoolT;
00184 typedef PoolT::iterator PoolIt;
00185
00187 Connection* find_mru();
00188 void remove_old_connections();
00189
00191 PoolT pool_;
00192 BeecryptMutex mutex_;
00193 };
00194
00195 }
00196
00197 #endif // !defined(MYSQLPP_CPOOL_H)
00198