libpqxx  7.9.1
Transactor framework

Sometimes a transaction can fail for completely transient reasons, such as a conflict with another transaction in SERIALIZABLE isolation. The right way to handle those failures is often just to re-run the transaction from scratch.

For example, your REST API might be handling each HTTP request in its own database transaction, and if this kind of transient failure happens, you simply want to "replay" the whole request, in a fresh transaction.

You won't necessarily want to execute the exact same SQL commands with the exact same data. Some of your SQL statements may depend on state that can vary between retries. Data in the database may already have changed, for instance. So instead of dumbly replaying the SQL, you re-run the same application code that produced those SQL commands, from the start.

The transactor framework makes it a little easier for you to do this safely, and avoid typical pitfalls. You encapsulate the work that you want to do into a callable that you pass to the perform function.

Here's how it works. You write your transaction code as a lambda or function, which creates its own transaction object, does its work, and commits at the end. You pass that callback to pqxx::perform, which runs it for you.

If there's a failure inside your callback, there will be an exception. Your transaction object goes out of scope and gets destroyed, so that it aborts implicitly. Seeing this, perform tries running your callback again. It stops doing that when the callback succeeds, or when it has failed too many times, or when there's an error that leaves the database in an unknown state, such as a lost connection just while we're waiting for the database to confirm a commit. It all depends on the type of exception.

The callback takes no arguments. If you're using lambdas, the easy way to pass arguments is for the lambda to "capture" them from your variables. Or, if you're using functions, you may want to use std::bind.

Once your callback succeeds, it can return a result, and perform will return that result back to you.