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 file in the top directory of
00008  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         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                 // Strict weak ordering for ConnectionInfo objects.
00171                 // 
00172                 // This ordering defines all in-use connections to be "less
00173                 // than" those not in use.  Within each group, connections
00174                 // less recently touched are less than those more recent.
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 } // end namespace mysqlpp
00196 
00197 #endif // !defined(MYSQLPP_CPOOL_H)
00198 

Generated on Fri Feb 29 16:26:00 2008 for MySQL++ by  doxygen 1.4.7