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 
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                 // Strict weak ordering for ConnectionInfo objects.
00227                 // 
00228                 // This ordering defines all in-use connections to be "less
00229                 // than" those not in use.  Within each group, connections
00230                 // less recently touched are less than those more recent.
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 } // end namespace mysqlpp
00253 
00254 #endif // !defined(MYSQLPP_CPOOL_H)
00255 

Generated on Thu Jun 3 11:59:12 2010 for MySQL++ by  doxygen 1.4.7