MySQL++  3.2.5
cpool.h
Go to the documentation of this file.
1 
4 /***********************************************************************
5  Copyright (c) 2007-2008 by Educational Technology Resources, Inc. and
6  (c) 2007 by Jonathan Wakely. Others may also hold copyrights on
7  code in this file. See the CREDITS.txt file in the top directory
8  of the distribution for details.
9 
10  This file is part of MySQL++.
11 
12  MySQL++ is free software; you can redistribute it and/or modify it
13  under the terms of the GNU Lesser General Public License as published
14  by the Free Software Foundation; either version 2.1 of the License, or
15  (at your option) any later version.
16 
17  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
18  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
20  License for more details.
21 
22  You should have received a copy of the GNU Lesser General Public
23  License along with MySQL++; if not, write to the Free Software
24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25  USA
26 ***********************************************************************/
27 
28 #if !defined(MYSQLPP_CPOOL_H)
29 #define MYSQLPP_CPOOL_H
30 
31 #include "beemutex.h"
32 
33 #include <list>
34 
35 #include <assert.h>
36 #include <time.h>
37 
38 namespace mysqlpp {
39 
40 #if !defined(DOXYGEN_IGNORE)
41 // Make Doxygen ignore this
42 class MYSQLPP_EXPORT Connection;
43 #endif
44 
67 
68 class MYSQLPP_EXPORT ConnectionPool
69 {
70 public:
72  ConnectionPool() { }
73 
78  virtual ~ConnectionPool() { assert(empty()); }
79 
81  bool empty() const { return pool_.empty(); }
82 
106  virtual Connection* exchange(const Connection* pc);
107 
120  virtual Connection* grab();
121 
137  virtual void release(const Connection* pc);
138 
150  void remove(const Connection* pc);
151 
162  virtual Connection* safe_grab();
163 
165  void shrink() { clear(false); }
166 
167 protected:
176  void clear(bool all = true);
177 
188  virtual Connection* create() = 0;
189 
197  virtual void destroy(Connection*) = 0;
198 
207  virtual unsigned int max_idle_time() = 0;
208 
210  size_t size() const { return pool_.size(); }
211 
212 private:
214  struct ConnectionInfo {
215  Connection* conn;
216  time_t last_used;
217  bool in_use;
218 
219  ConnectionInfo(Connection* c) :
220  conn(c),
221  last_used(time(0)),
222  in_use(true)
223  {
224  }
225 
226  // Strict weak ordering for ConnectionInfo objects.
227  //
228  // This ordering defines all in-use connections to be "less
229  // than" those not in use. Within each group, connections
230  // less recently touched are less than those more recent.
231  bool operator<(const ConnectionInfo& rhs) const
232  {
233  const ConnectionInfo& lhs = *this;
234  return lhs.in_use == rhs.in_use ?
235  lhs.last_used < rhs.last_used :
236  lhs.in_use;
237  }
238  };
239  typedef std::list<ConnectionInfo> PoolT;
240  typedef PoolT::iterator PoolIt;
241 
243  Connection* find_mru();
244  void remove(const PoolIt& it);
245  void remove_old_connections();
246 
248  PoolT pool_;
249  BeecryptMutex mutex_;
250 };
251 
252 } // end namespace mysqlpp
253 
254 #endif // !defined(MYSQLPP_CPOOL_H)
255 
mysqlpp::ConnectionPool::destroy
virtual void destroy(Connection *)=0
Destroy a connection.
beemutex.h
MUTually EXclusive lock class.
mysqlpp::Connection
Manages the connection to the database server.
Definition: connection.h:81
mysqlpp::ConnectionPool::exchange
virtual Connection * exchange(const Connection *pc)
Return a defective connection to the pool and get a new one back.
Definition: cpool.cpp:117
cpool.h
Declares the ConnectionPool class.
mysqlpp::TooOld
Functor to test whether a given ConnectionInfo object is "too old".
Definition: cpool.cpp:69
mysqlpp::ConnectionPool::max_idle_time
virtual unsigned int max_idle_time()=0
Returns the maximum number of seconds a connection is able to remain idle before it is dropped.
mysqlpp::ScopedLock
Wrapper around BeecryptMutex to add scope-bound locking and unlocking.
Definition: beemutex.h:119
mysqlpp::ConnectionPool::safe_grab
virtual Connection * safe_grab()
Grab a free connection from the pool, testing that it's connected before returning it.
Definition: cpool.cpp:234
mysqlpp::ConnectionPool::release
virtual void release(const Connection *pc)
Return a connection to the pool.
Definition: cpool.cpp:168
connection.h
Declares the Connection class.
mysqlpp::ConnectionPool::create
virtual Connection * create()=0
Create a new connection.
mysqlpp::ConnectionPool::grab
virtual Connection * grab()
Grab a free connection from the pool.
Definition: cpool.cpp:150
mysqlpp::ConnectionPool::clear
void clear(bool all=true)
Drains the pool, freeing all allocated memory.
Definition: cpool.cpp:96
mysqlpp::ConnectionPool::remove
void remove(const Connection *pc)
Removes the given connection from the pool.
Definition: cpool.cpp:194