aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-02-21 10:42:13 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-02-21 10:42:13 +0200
commitcfc9c1fa3cab07396b38d192da3d0b6bfa36d0f5 (patch)
tree4070182474950d34f62fe5f2a828e160540753e6
parent4acabe57939ff37bad0a8aabc5164b6d5add449b (diff)
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.
-rw-r--r--odb/mysql/connection-factory.cxx7
-rw-r--r--odb/mysql/connection.cxx1
-rw-r--r--odb/mysql/connection.hxx14
-rw-r--r--odb/mysql/error.cxx11
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);