From ae4b24d8d83a46dce969fd049a480bcaaeb0ad83 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 24 Aug 2011 13:42:04 +0200 Subject: Add support for transaction multiplexing Also delay getting a connection until after we do all the sanity checks (e.g., that there is no active transaction). Otherwise we are running risk of getting blocked rather than throwing an exception. --- odb/pgsql/connection.cxx | 3 --- odb/pgsql/database.cxx | 6 ++++++ odb/pgsql/database.hxx | 2 +- odb/pgsql/database.ixx | 6 ------ odb/pgsql/transaction-impl.cxx | 29 ++++++++++++++++++++++++----- odb/pgsql/transaction-impl.hxx | 9 +++++---- odb/pgsql/transaction.hxx | 5 +++++ odb/pgsql/transaction.ixx | 6 ++++++ 8 files changed, 47 insertions(+), 19 deletions(-) diff --git a/odb/pgsql/connection.cxx b/odb/pgsql/connection.cxx index 808cdbb..0a23ed7 100644 --- a/odb/pgsql/connection.cxx +++ b/odb/pgsql/connection.cxx @@ -73,9 +73,6 @@ namespace odb transaction_impl* connection:: begin () { - if (transaction::has_current ()) - throw already_in_transaction (); - return new transaction_impl (connection_ptr (inc_ref (this))); } diff --git a/odb/pgsql/database.cxx b/odb/pgsql/database.cxx index 050961f..8d8b797 100644 --- a/odb/pgsql/database.cxx +++ b/odb/pgsql/database.cxx @@ -208,6 +208,12 @@ namespace odb { } + transaction_impl* database:: + begin () + { + return new transaction_impl (*this); + } + odb::connection* database:: connection_ () { diff --git a/odb/pgsql/database.hxx b/odb/pgsql/database.hxx index d3d8f7d..1b688fc 100644 --- a/odb/pgsql/database.hxx +++ b/odb/pgsql/database.hxx @@ -83,7 +83,7 @@ namespace odb // Transactions. // public: - transaction_impl* + virtual transaction_impl* begin (); public: diff --git a/odb/pgsql/database.ixx b/odb/pgsql/database.ixx index 467f765..527e336 100644 --- a/odb/pgsql/database.ixx +++ b/odb/pgsql/database.ixx @@ -16,11 +16,5 @@ namespace odb return connection_ptr ( static_cast (connection_ ())); } - - inline transaction_impl* database:: - begin () - { - return connection ()->begin (); - } } } diff --git a/odb/pgsql/transaction-impl.cxx b/odb/pgsql/transaction-impl.cxx index 81aaf08..c3ce31d 100644 --- a/odb/pgsql/transaction-impl.cxx +++ b/odb/pgsql/transaction-impl.cxx @@ -19,14 +19,15 @@ namespace odb namespace pgsql { transaction_impl:: + transaction_impl (database_type& db) + : odb::transaction_impl (db) + { + } + + transaction_impl:: transaction_impl (connection_ptr c) : odb::transaction_impl (c->database (), *c), connection_ (c) { - result_ptr r (PQexec (connection_->handle (), "begin")); - PGresult* h (r.get ()); - - if (!h || PGRES_COMMAND_OK != PQresultStatus (h)) - translate_error (*connection_, h); } transaction_impl:: @@ -35,6 +36,24 @@ namespace odb } void transaction_impl:: + start () + { + // Grab a connection if we don't already have one. + // + if (connection_ == 0) + { + connection_ = static_cast (database_).connection (); + odb::transaction_impl::connection_ = connection_.get (); + } + + result_ptr r (PQexec (connection_->handle (), "begin")); + PGresult* h (r.get ()); + + if (!h || PGRES_COMMAND_OK != PQresultStatus (h)) + translate_error (*connection_, h); + } + + void transaction_impl:: commit () { result_ptr r (PQexec (connection_->handle (), "commit")); diff --git a/odb/pgsql/transaction-impl.hxx b/odb/pgsql/transaction-impl.hxx index d3a76ce..2551994 100644 --- a/odb/pgsql/transaction-impl.hxx +++ b/odb/pgsql/transaction-impl.hxx @@ -23,19 +23,20 @@ namespace odb { class LIBODB_PGSQL_EXPORT transaction_impl: public odb::transaction_impl { - protected: - friend class connection; - friend class transaction; - + public: typedef pgsql::database database_type; typedef pgsql::connection connection_type; + transaction_impl (database_type&); transaction_impl (connection_ptr); virtual ~transaction_impl (); virtual void + start (); + + virtual void commit (); virtual void diff --git a/odb/pgsql/transaction.hxx b/odb/pgsql/transaction.hxx index 450842d..39675d8 100644 --- a/odb/pgsql/transaction.hxx +++ b/odb/pgsql/transaction.hxx @@ -46,6 +46,11 @@ namespace odb static transaction& current (); + // Set the current thread's transaction. + // + static void + current (transaction&); + public: transaction_impl& implementation (); diff --git a/odb/pgsql/transaction.ixx b/odb/pgsql/transaction.ixx index d109e97..42fe778 100644 --- a/odb/pgsql/transaction.ixx +++ b/odb/pgsql/transaction.ixx @@ -37,5 +37,11 @@ namespace odb { return implementation ().connection (); } + + inline void transaction:: + current (transaction& t) + { + odb::transaction::current (t); + } } } -- cgit v1.1