diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-12-01 12:09:07 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-12-01 12:09:07 +0200 |
commit | dce9ac4635ec667a27712ad4b0251c1a2ca5df06 (patch) | |
tree | 43a803f08fd4b81493c0f72bcef7727e08d44c1a | |
parent | ece02b4c8cd6f1a332afdb4655b675d1193ed9c2 (diff) |
Detect and mark connection as failed
-rw-r--r-- | odb/pgsql/connection-factory.cxx | 7 | ||||
-rw-r--r-- | odb/pgsql/connection.cxx | 4 | ||||
-rw-r--r-- | odb/pgsql/connection.hxx | 14 | ||||
-rw-r--r-- | odb/pgsql/error.cxx | 23 | ||||
-rw-r--r-- | odb/pgsql/error.hxx | 6 |
5 files changed, 28 insertions, 26 deletions
diff --git a/odb/pgsql/connection-factory.cxx b/odb/pgsql/connection-factory.cxx index d725ef0..2154b54 100644 --- a/odb/pgsql/connection-factory.cxx +++ b/odb/pgsql/connection-factory.cxx @@ -126,9 +126,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/pgsql/connection.cxx b/odb/pgsql/connection.cxx index b173136..c5599e8 100644 --- a/odb/pgsql/connection.cxx +++ b/odb/pgsql/connection.cxx @@ -30,7 +30,7 @@ namespace odb { connection:: connection (database_type& db) - : odb::connection (db), db_ (db) + : odb::connection (db), db_ (db), failed_ (false) { handle_.reset (PQconnectdb (db.conninfo ().c_str ())); @@ -44,7 +44,7 @@ namespace odb connection:: connection (database_type& db, PGconn* handle) - : odb::connection (db), db_ (db), handle_ (handle) + : odb::connection (db), db_ (db), handle_ (handle), failed_ (false) { init (); } diff --git a/odb/pgsql/connection.hxx b/odb/pgsql/connection.hxx index 39d2912..b26e116 100644 --- a/odb/pgsql/connection.hxx +++ b/odb/pgsql/connection.hxx @@ -81,6 +81,19 @@ namespace odb using odb::connection::tracer; public: + bool + failed () const + { + return failed_; + } + + void + mark_failed () + { + failed_ = true; + } + + public: PGconn* handle () { @@ -108,6 +121,7 @@ namespace odb database_type& db_; auto_handle<PGconn> handle_; + bool failed_; // Keep statement_cache_ after handle_ so that it is destroyed before // the connection is closed. diff --git a/odb/pgsql/error.cxx b/odb/pgsql/error.cxx index 149654d..ae9cbc2 100644 --- a/odb/pgsql/error.cxx +++ b/odb/pgsql/error.cxx @@ -17,26 +17,15 @@ namespace odb namespace pgsql { void - translate_error (connection& c) - { - PGconn* h (c.handle ()); - - assert (CONNECTION_BAD == PQstatus (h)); - - if (const char* m = PQerrorMessage (h)) - throw database_exception (m); - else - throw database_exception ("unknown error"); - } - - void - translate_error (connection& c, - PGresult* r) + translate_error (connection& c, PGresult* r) { if (!r) { if (CONNECTION_BAD == PQstatus (c.handle ())) + { + c.mark_failed (); throw connection_lost (); + } else throw bad_alloc (); } @@ -66,8 +55,10 @@ namespace odb throw deadlock (); else if (CONNECTION_BAD == PQstatus (c.handle ())) + { + c.mark_failed (); throw connection_lost (); - + } else throw database_exception (ss, error_message); } diff --git a/odb/pgsql/error.hxx b/odb/pgsql/error.hxx index 865cf0d..018c5fd 100644 --- a/odb/pgsql/error.hxx +++ b/odb/pgsql/error.hxx @@ -11,17 +11,13 @@ #include <libpq-fe.h> #include <odb/pgsql/version.hxx> +#include <odb/pgsql/forward.hxx> // connection #include <odb/pgsql/details/export.hxx> namespace odb { namespace pgsql { - class connection; - - LIBODB_PGSQL_EXPORT void - translate_error (connection&); - // Translate an error condition involving a PGresult*. If r is null, it is // assumed that the error was caused due to a bad connection or a memory // allocation error. |