Author: wyoung
Date: Fri Dec 28 10:23:03 2007
New Revision: 2034
URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2034&view=rev
Log:
- Made ConnectionPool::clear() protected, and gave it a parameter
allowing it to act in two modes: either remove everything (the
default, just like STL) or remove only unused connections.
- Added shrink() public method, wrapping clear(false)
Modified:
trunk/lib/cpool.cpp
trunk/lib/cpool.h
Modified: trunk/lib/cpool.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/cpool.cpp?rev=2034&r1=2033&r2=2034&view=diff
==============================================================================
--- trunk/lib/cpool.cpp (original)
+++ trunk/lib/cpool.cpp Fri Dec 28 10:23:03 2007
@@ -62,17 +62,26 @@
//// clear /////////////////////////////////////////////////////////////
-// Destroy all connections in the pool and drain pool.
+// Destroy connections in the pool, either all of them (completely
+// draining the pool) or just those not currently in use. The public
+// method shrink() is an alias for clear(false).
void
-ConnectionPool::clear()
+ConnectionPool::clear(bool all)
{
ScopedLock lock(mutex_); // ensure we're not interfered with
- for (PoolIt it = pool_.begin(); it != pool_.end(); ++it) {
- destroy(it->conn);
+ PoolIt it = pool_.begin(), doomed;
+ while (it != pool_.end()) {
+ if (all || !it->in_use) {
+ doomed = it++;
+ destroy(doomed->conn);
+ pool_.erase(doomed);
+ }
+ else {
+ ++it;
+ }
}
- pool_.clear();
}
Modified: trunk/lib/cpool.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/cpool.h?rev=2034&r1=2033&r2=2034&view=diff
==============================================================================
--- trunk/lib/cpool.h (original)
+++ trunk/lib/cpool.h Fri Dec 28 10:23:03 2007
@@ -70,14 +70,6 @@
/// calling clear() in its dtor.
virtual ~ConnectionPool() { assert(pool_.empty()); }
- /// \brief Drains the pool, freeing all allocated memory.
- ///
- /// A derived class must call this in its dtor to avoid leaking all
- /// Connection objects still in existence. We can't do it up at
- /// this level because this class's dtor can't call our subclass's
- /// destroy() method.
- void clear();
-
/// \brief Grab a free connection from the pool.
///
/// This method creates a new connection if an unused one doesn't
@@ -105,7 +97,21 @@
/// when idle, so you might as well not be using a pool.
void release(const Connection* pc);
+ /// \brief Remove all unused connections from the pool
+ void shrink() { clear(false); }
+
protected:
+ //// Subclass interface
+ /// \brief Drains the pool, freeing all allocated memory.
+ ///
+ /// A derived class must call this in its dtor to avoid leaking all
+ /// Connection objects still in existence. We can't do it up at
+ /// this level because this class's dtor can't call our subclass's
+ /// destroy() method.
+ ///
+ /// \param all if true, remove all connections, even those in use
+ void clear(bool all = true);
+
//// Subclass overrides
/// \brief Create a new connection
///
_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits