Author: wyoung
Date: Fri Apr 11 17:43:21 2008
New Revision: 2271
URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2271&view=rev
Log:
Overriding grab() and release() in SimpleConnectionPool in
examples/cpool.cpp to show how to do connection-in-use count limiting.
Also, added a few more output indicator states to allow one to better
understand program flow.
Modified:
trunk/Wishlist
trunk/examples/cpool.cpp
trunk/lib/cpool.h
Modified: trunk/Wishlist
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/Wishlist?rev=2271&r1=2270&r2=2271&view=diff
==============================================================================
--- trunk/Wishlist (original)
+++ trunk/Wishlist Fri Apr 11 17:43:21 2008
@@ -7,10 +7,6 @@
o Any time you must hand-roll some SQL code in your program,
consider whether it could be generalized to a widely-useful
API feature.
-
-v3.0
-----
- o Limit the number of active connections in examples/cpool.cpp.
v3.1 Tentative Plan
Modified: trunk/examples/cpool.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/examples/cpool.cpp?rev=2271&r1=2270&r2=2271&view=diff
==============================================================================
--- trunk/examples/cpool.cpp (original)
+++ trunk/examples/cpool.cpp Fri Apr 11 17:43:21 2008
@@ -45,6 +45,7 @@
// The object's only constructor
SimpleConnectionPool(const char* db, const char* server,
const char* user, const char* password) :
+ conns_in_use_(0),
db_(db ? db : ""),
server_(server ? server : ""),
user_(user ? user : ""),
@@ -57,6 +58,29 @@
~SimpleConnectionPool()
{
clear();
+ }
+
+ // Do a simple form of in-use connection limiting: wait to return
+ // a connection until there are a reasonably low number in use
+ // already. Can't do this in create() because we're interested in
+ // connections actually in use, not those created. Also note that
+ // we keep our own count; ConnectionPool::size() isn't the same!
+ mysqlpp::Connection* grab()
+ {
+ while (conns_in_use_ > 8) {
+ cout.put('R'); cout.flush(); // indicate waiting for
release
+ sleep(1);
+ }
+
+ ++conns_in_use_;
+ return mysqlpp::ConnectionPool::grab();
+ }
+
+ // Other half of in-use conn count limit
+ void release(const mysqlpp::Connection* pc)
+ {
+ mysqlpp::ConnectionPool::release(pc);
+ --conns_in_use_;
}
protected:
@@ -91,6 +115,9 @@
}
private:
+ // Number of connections currently in use
+ unsigned int conns_in_use_;
+
// Our connection parameters
std::string db_, server_, user_, password_;
};
@@ -110,6 +137,7 @@
// show good style, so we take the high road and ensure the
// resources are allocated before we do any queries.
mysqlpp::Connection::thread_start();
+ cout.put('S'); cout.flush(); // indicate thread started
// Pull data from the sample table a bunch of times, releasing the
// connection we use each time.
@@ -142,6 +170,7 @@
// Tell main() that this thread is no longer running
*reinterpret_cast<bool*>(running_flag) = false;
+ cout.put('E'); cout.flush(); // indicate thread ended
// Release the per-thread resources before we exit
mysqlpp::Connection::thread_end();
Modified: trunk/lib/cpool.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/cpool.h?rev=2271&r1=2270&r2=2271&view=diff
==============================================================================
--- trunk/lib/cpool.h (original)
+++ trunk/lib/cpool.h Fri Apr 11 17:43:21 2008
@@ -92,7 +92,7 @@
/// lifetime of connection objects it creates.
///
/// \retval a pointer to the connection
- Connection* grab();
+ virtual Connection* grab();
/// \brief Return a connection to the pool
///
_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits