Skip to content

Commit

Permalink
Refactor rollback handling in dtor
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Jan 2, 2025
1 parent 1876501 commit 7374901
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 35 deletions.
1 change: 1 addition & 0 deletions include/tao/pq/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace tao::pq
friend class connection_pool;
friend class table_reader;
friend class table_writer;
friend class transaction;
friend class transaction_base;

friend class internal::top_level_transaction;
Expand Down
6 changes: 5 additions & 1 deletion include/tao/pq/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace tao::pq
{
class connection;
class transaction;

struct log
{
Expand Down Expand Up @@ -120,7 +121,10 @@ namespace tao::pq

struct transaction_t
{
// TODO...
// check std::current_exception() for more information
using destructor_rollback_failed_t = std::function< void( transaction& ) >; // noexcept

destructor_rollback_failed_t destructor_rollback_failed;

} transaction;
};
Expand Down
2 changes: 2 additions & 0 deletions include/tao/pq/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace tao::pq

virtual void v_reset() noexcept = 0;

void rollback_in_dtor() noexcept;

public:
[[nodiscard]] auto subtransaction() -> std::shared_ptr< transaction >;
[[nodiscard]] auto pipeline() -> std::shared_ptr< pq::pipeline >;
Expand Down
13 changes: 1 addition & 12 deletions src/lib/pq/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <cctype>
#include <chrono>
#include <cstring>
#include <exception>
#include <format>
#include <functional>
#include <memory>
Expand Down Expand Up @@ -137,17 +136,7 @@ namespace tao::pq
~top_level_transaction() override
{
if( m_connection && m_connection->attempt_rollback() ) {
try {
rollback();
}
// LCOV_EXCL_START
catch( const std::exception& ) { // NOLINT(bugprone-empty-catch)
// TAO_LOG( WARNING, "unable to rollback transaction, swallowing exception: " + std::string( e.what() ) );
}
catch( ... ) { // NOLINT(bugprone-empty-catch)
// TAO_LOG( WARNING, "unable to rollback transaction, swallowing unknown exception" );
}
// LCOV_EXCL_STOP
rollback_in_dtor();
}
}

Expand Down
42 changes: 20 additions & 22 deletions src/lib/pq/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <chrono>
#include <cstdio>
#include <exception>
#include <memory>
#include <stdexcept>
#include <utility>
Expand All @@ -33,17 +32,7 @@ namespace tao::pq
~top_level_subtransaction() override
{
if( m_connection && m_connection->attempt_rollback() ) {
try {
rollback();
}
// LCOV_EXCL_START
catch( const std::exception& ) { // NOLINT(bugprone-empty-catch)
// TAO_LOG( WARNING, "unable to rollback transaction, swallowing exception: " + std::string( e.what() ) );
}
catch( ... ) { // NOLINT(bugprone-empty-catch)
// TAO_LOG( WARNING, "unable to rollback transaction, swallowing unknown exception" );
}
// LCOV_EXCL_STOP
rollback_in_dtor();
}
}

Expand Down Expand Up @@ -79,14 +68,7 @@ namespace tao::pq
~nested_subtransaction() override
{
if( m_connection && m_connection->attempt_rollback() ) {
try {
rollback();
}
// LCOV_EXCL_START
catch( ... ) { // NOLINT(bugprone-empty-catch)
// TODO: How to handle this case properly?
}
// LCOV_EXCL_STOP
rollback_in_dtor();
}
}

Expand Down Expand Up @@ -130,8 +112,8 @@ namespace tao::pq

void transaction::commit()
{
check_current_transaction();
try {
check_current_transaction();
v_commit();
}
// LCOV_EXCL_START
Expand All @@ -145,8 +127,8 @@ namespace tao::pq

void transaction::rollback()
{
check_current_transaction();
try {
check_current_transaction();
v_rollback();
}
// LCOV_EXCL_START
Expand All @@ -158,4 +140,20 @@ namespace tao::pq
v_reset();
}

void transaction::rollback_in_dtor() noexcept
{
try {
check_current_transaction();
v_rollback();
}
// LCOV_EXCL_START
catch( ... ) {
if( m_connection->m_log && m_connection->m_log->transaction.destructor_rollback_failed ) {
m_connection->m_log->transaction.destructor_rollback_failed( *this );
}
}
// LCOV_EXCL_STOP
v_reset();
}

} // namespace tao::pq

0 comments on commit 7374901

Please sign in to comment.