diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-12-29 05:16:51 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-12-29 06:00:08 +0000 |
commit | 50a3dbfd0fa9dbcdf542d64be26d6f44ba65998c (patch) | |
tree | 2c00d0fc906e2b246ca2df29e2dc61580cb37444 /libodbcpp/odbc-connection.cpp | |
parent | Add missing override attribute (diff) | |
download | libdbpp-odbc-1.0.0.tar.bz2 libdbpp-odbc-1.0.0.tar.xz libdbpp-odbc-1.0.0.zip |
Improve and centralise transaction handling logiclibdbpp-odbc-1.0.0
Diffstat (limited to 'libodbcpp/odbc-connection.cpp')
-rw-r--r-- | libodbcpp/odbc-connection.cpp | 100 |
1 files changed, 23 insertions, 77 deletions
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 |