Skip to content

Commit

Permalink
Address a few more findings.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtv committed Nov 25, 2023
1 parent 47599db commit 5c33a82
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
49 changes: 24 additions & 25 deletions src/robusttransaction.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -171,40 +171,39 @@ void pqxx::internal::basic_robusttransaction::do_commit()

// If we get here, we're in doubt. Figure out what happened.

int const max_attempts{500};
constexpr int max_attempts{500};
constexpr unsigned int wait_micros{300u};
static_assert(max_attempts > 0);

tx_stat stat;
for (int attempts{0}; attempts < max_attempts;
++attempts, pqxx::internal::wait_for(300u))
++attempts, pqxx::internal::wait_for(wait_micros))
{
stat = tx_unknown;
try
{
stat = query_status(m_xid, m_conn_string);
switch (query_status(m_xid, m_conn_string))
{
case tx_unknown:
// We were unable to reconnect and query transaction status.
// Stay in it for another attempt.
return;
case tx_committed:
// Success! We're done.
return;
case tx_aborted:
// Aborted. We're done.
do_abort();
return;
case tx_in_progress:
// The transaction is still running. Stick around until we know what
// transpires.
break;
default: PQXX_UNREACHABLE;
}
}
catch (pqxx::broken_connection const &)
{
// Swallow the error. Pause and retry.
}
switch (stat)
{
case tx_unknown:
// We were unable to reconnect and query transaction status.
// Stay in it for another attempt.
return;
case tx_committed:
// Success! We're done.
return;
case tx_aborted:
// Aborted. We're done.
do_abort();
return;
case tx_in_progress:
// The transaction is still running. Stick around until we know what
// transpires.
break;
default: PQXX_UNREACHABLE;
// We can expect this to happen before we can get a working
// connection. Swallow the error and retry.
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/sql_cursor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ find_query_end(std::string_view query, pqxx::internal::encoding_group enc)
{
auto const text{std::data(query)};
auto const size{std::size(query)};
std::string::size_type end;
std::string::size_type end{size};
if (enc == pqxx::internal::encoding_group::MONOBYTE)
{
// This is an encoding where we can scan backwards from the end.
for (end = std::size(query); end > 0 and useless_trail(text[end - 1]);
--end)
;
// C++20: Use string_view::ends_with() and sub-view.
while (end > 0 and useless_trail(text[end - 1]))
--end;
}
else
{
Expand Down Expand Up @@ -135,11 +135,10 @@ pqxx::internal::sql_cursor::sql_cursor(
m_home{t.conn()},
m_empty_result{},
m_adopted{true},
m_ownership{op},
m_at_end{0},
m_pos{-1}
{
m_adopted = true;
m_ownership = op;
}


Expand Down

0 comments on commit 5c33a82

Please sign in to comment.