From f5d79a0cd5dc12a430d17bbdca7ec5aacb45996c 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/transaction.hxx | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) (limited to 'odb/transaction.hxx') diff --git a/odb/transaction.hxx b/odb/transaction.hxx index c2b1f77..0d4f518 100644 --- a/odb/transaction.hxx +++ b/odb/transaction.hxx @@ -22,8 +22,11 @@ namespace odb typedef odb::database database_type; typedef odb::connection connection_type; + // If the second argument is false, then this transaction is not + // made the current transaction of the thread. + // explicit - transaction (transaction_impl*); + transaction (transaction_impl*, bool make_current = true); // Unless the transaction has already been finalized (explicitly // committed or rolled back), the destructor will roll it back. @@ -46,16 +49,26 @@ namespace odb connection_type& connection (); + // Return true if there is a transaction in effect. + // + static bool + has_current (); + // Return current transaction or throw if there is no transaction // in effect. // static transaction& current (); - // Return true if there is a transaction in effect. + // Set the current thread's transaction. // - static bool - has_current (); + static void + current (transaction&); + + // Revert to the no transaction in effect state for the current thread. + // + static void + reset_current (); public: transaction_impl& @@ -74,21 +87,17 @@ namespace odb class LIBODB_EXPORT transaction_impl { - protected: - friend class transaction; - + public: typedef odb::database database_type; typedef odb::connection connection_type; - transaction_impl (database_type& db, connection_type& c) - : database_ (db), connection_ (c) - { - } - virtual ~transaction_impl (); virtual void + start () = 0; + + virtual void commit () = 0; virtual void @@ -103,12 +112,23 @@ namespace odb connection_type& connection () { - return connection_; + return *connection_; + } + + protected: + transaction_impl (database_type& db) + : database_ (db), connection_ (0) + { + } + + transaction_impl (database_type& db, connection_type& c) + : database_ (db), connection_ (&c) + { } protected: database_type& database_; - connection_type& connection_; + connection_type* connection_; }; } -- cgit v1.1