From cfc9c1fa3cab07396b38d192da3d0b6bfa36d0f5 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 21 Feb 2011 10:42:13 +0200 Subject: Don't reuse failed connections If MySQL API returns an error indicating the connection is no longer usable, mark it as failed. In connection_pool_factory free failed connections instead of returning them to the pool. --- odb/mysql/connection-factory.cxx | 7 ++++--- odb/mysql/connection.cxx | 1 + odb/mysql/connection.hxx | 14 ++++++++++++++ odb/mysql/error.cxx | 11 ++++++++++- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/odb/mysql/connection-factory.cxx b/odb/mysql/connection-factory.cxx index dce0f97..b9b3d19 100644 --- a/odb/mysql/connection-factory.cxx +++ b/odb/mysql/connection-factory.cxx @@ -230,9 +230,10 @@ namespace odb // Determine if we need to keep or free this connection. // - bool keep (waiters_ != 0 || - min_ == 0 || - (connections_.size () + in_use_ <= min_)); + bool keep (!c->failed () && + (waiters_ != 0 || + min_ == 0 || + (connections_.size () + in_use_ <= min_))); in_use_--; diff --git a/odb/mysql/connection.cxx b/odb/mysql/connection.cxx index 1395592..7679e27 100644 --- a/odb/mysql/connection.cxx +++ b/odb/mysql/connection.cxx @@ -20,6 +20,7 @@ namespace odb connection:: connection (database_type& db) : db_ (db), + failed_ (false), handle_ (&mysql_), active_ (0), statement_cache_ (new statement_cache_type (*this)) diff --git a/odb/mysql/connection.hxx b/odb/mysql/connection.hxx index 0310ee6..6704f23 100644 --- a/odb/mysql/connection.hxx +++ b/odb/mysql/connection.hxx @@ -46,6 +46,19 @@ namespace odb } public: + bool + failed () const + { + return failed_; + } + + void + mark_failed () + { + failed_ = true; + } + + public: MYSQL* handle () { @@ -91,6 +104,7 @@ namespace odb private: database_type& db_; + bool failed_; MYSQL mysql_; MYSQL* handle_; diff --git a/odb/mysql/error.cxx b/odb/mysql/error.cxx index 460816a..94a41b2 100644 --- a/odb/mysql/error.cxx +++ b/odb/mysql/error.cxx @@ -16,7 +16,7 @@ namespace odb namespace mysql { void - translate_error (connection&, + translate_error (connection& c, unsigned int e, const std::string& sqlstate, const std::string& message) @@ -31,6 +31,15 @@ namespace odb { throw deadlock (); } + case CR_SERVER_LOST: + case CR_SERVER_GONE_ERROR: + case CR_UNKNOWN_ERROR: + { + // This connection is no longer usable. + // + c.mark_failed (); + // Fall through. + } default: { throw database_exception (e, sqlstate, message); -- cgit v1.1