diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-08-24 13:42:04 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-08-24 13:42:04 +0200 |
commit | ae4b24d8d83a46dce969fd049a480bcaaeb0ad83 (patch) | |
tree | ca9d382e5a6516ac032d491969e08948a246537d | |
parent | 680b7c42d86919169d04dbd96e41adeca61c6782 (diff) |
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.
-rw-r--r-- | odb/pgsql/connection.cxx | 3 | ||||
-rw-r--r-- | odb/pgsql/database.cxx | 6 | ||||
-rw-r--r-- | odb/pgsql/database.hxx | 2 | ||||
-rw-r--r-- | odb/pgsql/database.ixx | 6 | ||||
-rw-r--r-- | odb/pgsql/transaction-impl.cxx | 29 | ||||
-rw-r--r-- | odb/pgsql/transaction-impl.hxx | 9 | ||||
-rw-r--r-- | odb/pgsql/transaction.hxx | 5 | ||||
-rw-r--r-- | 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<pgsql::connection*> (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_type&> (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); + } } } |