From 50a3dbfd0fa9dbcdf542d64be26d6f44ba65998c 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 --- libodbcpp/odbc-connection.cpp | 100 +++++++++------------------------------ libodbcpp/odbc-connection.h | 14 ++---- libodbcpp/odbc-modifycommand.cpp | 4 +- 3 files changed, 29 insertions(+), 89 deletions(-) (limited to 'libodbcpp') diff --git a/libodbcpp/odbc-connection.cpp b/libodbcpp/odbc-connection.cpp index 433acb5..c26af05 100644 --- a/libodbcpp/odbc-connection.cpp +++ b/libodbcpp/odbc-connection.cpp @@ -13,9 +13,7 @@ ODBC::Connection::Connection(const DSN& d) : env(0), conn(0), thinkDelStyle(DB::BulkDeleteUsingUsing), - thinkUpdStyle(DB::BulkUpdateUsingFromSrc), - txDepth(0), - txAborted(false) + thinkUpdStyle(DB::BulkUpdateUsingFromSrc) { connectPre(); RETCODE dberr = SQLConnect(conn, (SQLCHAR*)d.dsn.c_str(), SQL_NTS, @@ -73,9 +71,7 @@ ODBC::Connection::Connection(const std::string & s) : env(0), conn(0), thinkDelStyle(DB::BulkDeleteUsingUsing), - thinkUpdStyle(DB::BulkUpdateUsingFromSrc), - txDepth(0), - txAborted(false) + thinkUpdStyle(DB::BulkUpdateUsingFromSrc) { connectPre(); RETCODE dberr = SQLDriverConnect(conn, NULL, (SQLCHAR*)s.c_str(), s.length(), NULL, 0, NULL, SQL_DRIVER_NOPROMPT); @@ -97,88 +93,38 @@ ODBC::Connection::~Connection() } void -ODBC::Connection::finish() const +ODBC::Connection::beginTxInt() { - if (txDepth != 0) { - rollbackTx(); - throw DB::TransactionStillOpen(); - } -} - -int -ODBC::Connection::beginTx() const -{ - if (txDepth == 0) { - SQLRETURN dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF); - if (!SQL_SUCCEEDED(dberr)) { - throw Error(dberr, SQL_HANDLE_DBC, conn); - } + SQLRETURN dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF); + if (!SQL_SUCCEEDED(dberr)) { + throw Error(dberr, SQL_HANDLE_DBC, conn); } - txDepth += 1; - return txDepth; } -int -ODBC::Connection::commitTx() const +void +ODBC::Connection::commitTxInt() { - if (txDepth > 0) { - if (txAborted) { - return rollbackTx(); - } - txDepth -= 1; - if (txDepth == 0) { - SQLRETURN dberr = SQLEndTran(SQL_HANDLE_DBC, conn, SQL_COMMIT); - if (!SQL_SUCCEEDED(dberr)) { - throw Error(dberr, SQL_HANDLE_DBC, conn); - } - dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON); - if (!SQL_SUCCEEDED(dberr)) { - throw Error(dberr, SQL_HANDLE_DBC, conn); - } - txAborted = false; - } - return txDepth; + SQLRETURN dberr = SQLEndTran(SQL_HANDLE_DBC, conn, SQL_COMMIT); + if (!SQL_SUCCEEDED(dberr)) { + throw Error(dberr, SQL_HANDLE_DBC, conn); } - throw DB::TransactionRequired(); -} - -int -ODBC::Connection::rollbackTx() const -{ - if (txDepth > 0) { - txDepth -= 1; - if (txDepth == 0) { - SQLRETURN dberr = SQLEndTran(SQL_HANDLE_DBC, conn, SQL_ROLLBACK); - if (!SQL_SUCCEEDED(dberr)) { - throw Error(dberr, SQL_HANDLE_DBC, conn); - } - dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON); - if (!SQL_SUCCEEDED(dberr)) { - throw Error(dberr, SQL_HANDLE_DBC, conn); - } - txAborted = false; - } - return txDepth; + dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON); + if (!SQL_SUCCEEDED(dberr)) { + throw Error(dberr, SQL_HANDLE_DBC, conn); } - throw DB::TransactionRequired(); } void -ODBC::Connection::abortTx() const +ODBC::Connection::rollbackTxInt() { - txAborted = true; -} - -bool -ODBC::Connection::txIsAborted() const -{ - return txAborted; -} - -bool -ODBC::Connection::inTx() const -{ - return (txDepth > 0); + SQLRETURN dberr = SQLEndTran(SQL_HANDLE_DBC, conn, SQL_ROLLBACK); + if (!SQL_SUCCEEDED(dberr)) { + throw Error(dberr, SQL_HANDLE_DBC, conn); + } + dberr = SQLSetConnectOption(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON); + if (!SQL_SUCCEEDED(dberr)) { + throw Error(dberr, SQL_HANDLE_DBC, conn); + } } DB::BulkDeleteStyle diff --git a/libodbcpp/odbc-connection.h b/libodbcpp/odbc-connection.h index 7fe5c56..97305e1 100644 --- a/libodbcpp/odbc-connection.h +++ b/libodbcpp/odbc-connection.h @@ -21,14 +21,10 @@ namespace ODBC { SQLHENV env; SQLHDBC conn; - void finish() const override; - int beginTx() const override; - int commitTx() const override; - int rollbackTx() const override; - void abortTx() const; - bool txIsAborted() const; - bool inTx() const override; - void ping() const override; + void beginTxInt() override; + void commitTxInt() override; + void rollbackTxInt() override; + void ping() const override; std::string getAttrStr(SQLINTEGER) const; SQLINTEGER getAttrInt(SQLINTEGER) const; DB::BulkDeleteStyle bulkDeleteStyle() const override; @@ -43,8 +39,6 @@ namespace ODBC { void connectPre(); void connectPost(); - mutable unsigned int txDepth; - mutable bool txAborted; }; } diff --git a/libodbcpp/odbc-modifycommand.cpp b/libodbcpp/odbc-modifycommand.cpp index cb6d3c0..0c49f92 100644 --- a/libodbcpp/odbc-modifycommand.cpp +++ b/libodbcpp/odbc-modifycommand.cpp @@ -16,11 +16,11 @@ unsigned int ODBC::ModifyCommand::execute(bool anc) { RETCODE rc = SQLExecute(hStmt); - if (!SQL_SUCCEEDED(rc)) { + if (!SQL_SUCCEEDED(rc)) { if (rc != SQL_NO_DATA || !anc) { throw Error(rc, SQL_HANDLE_STMT, hStmt); } - } + } SQLLEN rows; rc = SQLRowCount(hStmt, &rows); if (!SQL_SUCCEEDED(rc)) { -- cgit v1.2.3