cpool.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 /***********************************************************************
00005  Copyright (c) 2007-2008 by Educational Technology Resources, Inc. and
00006  (c) 2007 by Jonathan Wakely.  Others may also hold copyrights on
00007  code in this file.  See the CREDITS.txt file in the top directory
00008  of the distribution for details.
00009 
00010  This file is part of MySQL++.
00011 
00012  MySQL++ is free software; you can redistribute it and/or modify it
00013  under the terms of the GNU Lesser General Public License as published
00014  by the Free Software Foundation; either version 2.1 of the License, or
00015  (at your option) any later version.
00016 
00017  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
00018  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00019  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00020  License for more details.
00021 
00022  You should have received a copy of the GNU Lesser General Public
00023  License along with MySQL++; if not, write to the Free Software
00024  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
00025  USA
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 // Make Doxygen ignore this
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                 // Strict weak ordering for ConnectionInfo objects.
00174                 // 
00175                 // This ordering defines all in-use connections to be "less
00176                 // than" those not in use.  Within each group, connections
00177                 // less recently touched are less than those more recent.
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 } // end namespace mysqlpp
00199 
00200 #endif // !defined(MYSQLPP_CPOOL_H)
00201 

Generated on Wed Feb 4 14:42:56 2009 for MySQL++ by  doxygen 1.4.7