Author: mysqlpp
Date: Sun Apr 26 05:09:41 2009
New Revision: 2506

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2506&view=rev
Log:
Added ConnectionPool::safe_grab(), a wrapper around grab() and the new
release(it) method, pinging the connection before returning it.  If the
ping fails, we remove it from the pool and go back around again,
giving the best possible chance that the connection we return will be
usable.

Modified:
    trunk/examples/cpool.cpp
    trunk/lib/cpool.cpp
    trunk/lib/cpool.h

Modified: trunk/examples/cpool.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/examples/cpool.cpp?rev=2506&r1=2505&r2=2506&view=diff
==============================================================================
--- trunk/examples/cpool.cpp (original)
+++ trunk/examples/cpool.cpp Sun Apr 26 05:09:41 2009
@@ -3,7 +3,7 @@
        threads and POSIX threads.  Shows how to create and use a concrete
        ConnectionPool derivative.
 
- Copyright (c) 2008 by Educational Technology Resources, Inc.
+ Copyright (c) 2008-2009 by Educational Technology Resources, Inc.
  Others may also hold copyrights on code in this file.  See the
  CREDITS.txt file in the top directory of the distribution for details.
 
@@ -129,11 +129,11 @@
 {
        // Ask the underlying C API to allocate any per-thread resources it
        // needs, in case it hasn't happened already.  In this particular
-       // program, it's almost guaranteed that the grab() call below will
-       // create a new connection the first time through, and thus allocate
-       // these resources implicitly, but there's a nonzero chance that
-       // this won't happen.  Anyway, this is an example program, meant to
-       // show good style, so we take the high road and ensure the
+       // program, it's almost guaranteed that the safe_grab() call below
+       // will create a new connection the first time through, and thus
+       // allocate these resources implicitly, but there's a nonzero chance
+       // that this won't happen.  Anyway, this is an example program,
+       // meant to 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
@@ -143,7 +143,7 @@
        for (size_t i = 0; i < 6; ++i) {
                // Go get a free connection from the pool, or create a new one
                // if there are no free conns yet.
-               mysqlpp::Connection* cp = poolptr->grab();
+               mysqlpp::Connection* cp = poolptr->safe_grab();
                if (!cp) {
                        cerr << "Failed to get a connection from the pool!" << 
endl;
                        break;
@@ -198,7 +198,7 @@
        // awareness turned on.  See README-*.txt for your platform.
        poolptr = new SimpleConnectionPool(cmdline);
        try {
-               mysqlpp::Connection* cp = poolptr->grab();
+               mysqlpp::Connection* cp = poolptr->safe_grab();
                if (!cp->thread_aware()) {
                        cerr << "MySQL++ wasn't built with thread awareness!  " 
<<
                                        argv[0] << " can't run without it." << 
endl;

Modified: trunk/lib/cpool.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/cpool.cpp?rev=2506&r1=2505&r2=2506&view=diff
==============================================================================
--- trunk/lib/cpool.cpp (original)
+++ trunk/lib/cpool.cpp Sun Apr 26 05:09:41 2009
@@ -167,5 +167,22 @@
 }
 
 
+//// safe_grab /////////////////////////////////////////////////////////
+
+Connection*
+ConnectionPool::safe_grab()
+{
+       Connection* pc;
+       while (!(pc = grab())->ping()) {
+               for (PoolIt it = pool_.begin(); it != pool_.end(); ++it) {
+                       if (it->conn == pc) {
+                               remove(it);
+                               break;
+                       }
+               }
+       }
+}
+
+
 } // end namespace mysqlpp
 

Modified: trunk/lib/cpool.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/cpool.h?rev=2506&r1=2505&r2=2506&view=diff
==============================================================================
--- trunk/lib/cpool.h (original)
+++ trunk/lib/cpool.h Sun Apr 26 05:09:41 2009
@@ -108,6 +108,18 @@
        /// remove it from the pool.
        virtual void release(const Connection* pc);
 
+       /// \brief Grab a free connection from the pool, testing that it's
+       /// connected before returning it.
+       ///
+       /// This is just a wrapper around grab(), Connection::ping() and
+       /// release(), and is thus less efficient than grab().  Use it only
+       /// when it's possible for MySQL server connections to go away
+       /// unexpectedly, such as when the DB server can be restarted out
+       /// from under your application.
+       ///
+       /// \retval a pointer to the connection
+       virtual Connection* safe_grab();
+
        /// \brief Remove all unused connections from the pool
        void shrink() { clear(false); }
 


_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits

Reply via email to