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/transaction-impl.cxx | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'odb/pgsql/transaction-impl.cxx') 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")); -- cgit v1.1