- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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::empty
bool empty() const
Returns true if pool is empty.
Definition: cpool.h:81
mysqlpp::ConnectionPool::size
size_t size() const
Returns the current size of the internal connection pool.
Definition: cpool.h:210
mysqlpp::Connection
Manages the connection to the database server.
Definition: connection.h:59
mysqlpp::ConnectionPool
Manages a pool of connections for programs that need more than one Connection object at a time...
Definition: cpool.h:68
beemutex.h
MUTually EXclusive lock class.
mysqlpp::ConnectionPool::ConnectionPool
ConnectionPool()
Create empty pool.
Definition: cpool.h:72
mysqlpp::ConnectionPool::shrink
void shrink()
Remove all unused connections from the pool.
Definition: cpool.h:165
mysqlpp::ConnectionPool::~ConnectionPool
virtual ~ConnectionPool()
Destroy object.
Definition: cpool.h:78