From a9959465c36b04cbf2e0aaeca4ce78ca77e1a445 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 29 Dec 2015 05:16:51 +0000 Subject: Improve and centralise transaction handling logic --- libmysqlpp/my-connection.cpp | 57 ++++++++------------------------------ libmysqlpp/my-connection.h | 11 ++------ libmysqlpp/unittests/testmysql.cpp | 1 - 3 files changed, 15 insertions(+), 54 deletions(-) diff --git a/libmysqlpp/my-connection.cpp b/libmysqlpp/my-connection.cpp index c6ab993..687572f 100644 --- a/libmysqlpp/my-connection.cpp +++ b/libmysqlpp/my-connection.cpp @@ -58,9 +58,7 @@ NvpTarget(Opts) OptsTargetMap { }; -MySQL::Connection::Connection(const std::string & str) : - txDepth(0), - rolledback(false) +MySQL::Connection::Connection(const std::string & str) { std::stringstream i(str); Opts o; @@ -84,58 +82,27 @@ MySQL::Connection::~Connection() } void -MySQL::Connection::finish() const +MySQL::Connection::beginTxInt() { - if (txDepth != 0) { - rollbackTx(); - throw DB::TransactionStillOpen(); - } -} - -int -MySQL::Connection::beginTx() const -{ - if (txDepth == 0) { - if (mysql_autocommit(&conn, 0)) { - throw Error(&conn); - } - rolledback = false; + if (mysql_autocommit(&conn, 0)) { + throw Error(&conn); } - return ++txDepth; } -int -MySQL::Connection::commitTx() const +void +MySQL::Connection::commitTxInt() { - if (rolledback) { - return rollbackTx(); - } - if (--txDepth == 0) { - if (mysql_commit(&conn)) { - throw Error(&conn); - } + if (mysql_commit(&conn)) { + throw Error(&conn); } - return txDepth; } -int -MySQL::Connection::rollbackTx() const +void +MySQL::Connection::rollbackTxInt() { - if (--txDepth == 0) { - if (mysql_rollback(&conn)) { - throw Error(&conn); - } - } - else { - rolledback = true; + if (mysql_rollback(&conn)) { + throw Error(&conn); } - return txDepth; -} - -bool -MySQL::Connection::inTx() const -{ - return txDepth; } DB::BulkDeleteStyle diff --git a/libmysqlpp/my-connection.h b/libmysqlpp/my-connection.h index 238200e..2ffae0d 100644 --- a/libmysqlpp/my-connection.h +++ b/libmysqlpp/my-connection.h @@ -19,11 +19,9 @@ namespace MySQL { Connection(const std::string & info); ~Connection(); - void finish() const override; - int beginTx() const override; - int commitTx() const override; - int rollbackTx() const override; - bool inTx() const override; + void beginTxInt() override; + void commitTxInt() override; + void rollbackTxInt() override; void ping() const override; DB::BulkDeleteStyle bulkDeleteStyle() const override; DB::BulkUpdateStyle bulkUpdateStyle() const override; @@ -42,9 +40,6 @@ namespace MySQL { private: my_bool my_true; - mutable unsigned int txDepth; - mutable bool rolledback; - mutable boost::shared_ptr ctx; }; } diff --git a/libmysqlpp/unittests/testmysql.cpp b/libmysqlpp/unittests/testmysql.cpp index f8c46b6..00c15aa 100644 --- a/libmysqlpp/unittests/testmysql.cpp +++ b/libmysqlpp/unittests/testmysql.cpp @@ -55,7 +55,6 @@ BOOST_AUTO_TEST_CASE( bindAndSend ) mod->bindParamT(5, testInterval); mod->execute(); delete mod; - rw->commitTx(); delete rw; } -- cgit v1.2.3