From e0644a5b0af8b4655f52c52da1b7f57725504b3d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 15 Mar 2011 17:00:34 +0200 Subject: Avoid holding connection pool lock while pinging connection --- odb/mysql/connection-factory.cxx | 67 +++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/odb/mysql/connection-factory.cxx b/odb/mysql/connection-factory.cxx index bef21ce..225ef9a 100644 --- a/odb/mysql/connection-factory.cxx +++ b/odb/mysql/connection-factory.cxx @@ -169,41 +169,58 @@ namespace odb { tls_get (mysql_thread_init_); - lock l (mutex_); - + // The outer loop checks whether the connection we were + // given is still valid. + // while (true) { - // See if we have a spare connection. + shared_ptr c; + + lock l (mutex_); + + // The inner loop tries to find a free connection. // - if (connections_.size () != 0) + while (true) { - shared_ptr c (connections_.back ()); - connections_.pop_back (); + // See if we have a spare connection. + // + if (connections_.size () != 0) + { + c = connections_.back (); + connections_.pop_back (); - if (ping_ && !c->ping ()) - continue; + c->pool_ = this; + in_use_++; + break; + } - c->pool_ = this; - in_use_++; - return c; - } + // See if we can create a new one. + // + if(max_ == 0 || in_use_ < max_) + { + // For new connections we don't need to ping so we + // can return immediately. + // + shared_ptr c ( + new (shared) pooled_connection (*db_, this)); + in_use_++; + return c; + } - // See if we can create a new one. - // - if(max_ == 0 || in_use_ < max_) - { - shared_ptr c ( - new (shared) pooled_connection (*db_, this)); - in_use_++; - return c; + // Wait until someone releases a connection. + // + waiters_++; + cond_.wait (); + waiters_--; } - // Wait until someone releases a connection. - // - waiters_++; - cond_.wait (); - waiters_--; + l.unlock (); + + if (!ping_ || c->ping ()) + return c; } + + return shared_ptr (); // Never reached. } void connection_pool_factory:: -- cgit v1.1