Transactions
A transaction is a group of SQL statements that commit together or roll back together. In C++ the transaction should be an object: constructor BEGIN, commit() persists, destructor ROLLBACK if you never committed.
Detailed Explanation
You debit one account and credit another. If the process dies after the debit, you have lost money unless both writes were one transaction. BEGIN; statements; COMMIT is the success path. Any error, exception, or forgotten commit should ROLLBACK.
libpqxx pqxx::work is that object. SOCI soci::transaction. SQLite: sqlite3_exec(db, "BEGIN", ...), then COMMIT/ROLLBACK, which you should still wrap. Autocommit (each statement is its own transaction) is the default in SQLite and in libpqxx nontransaction. It is fine for a single insert. It is wrong for a two-step update.
Isolation: SQLite is serializable in the usual rollback journal sense (writers exclusive). Postgres isolation levels (read committed, repeatable read, serializable) change what concurrent transactions see. Pick the weakest level that is still correct; serializable is safest and can retry on conflicts (40001 in Postgres).
Keep transactions short. Do not hold a transaction open while you call HTTP or wait on a human. You hold locks (row or database). A long transaction is how SQLite writers queue and how Postgres vacuum falls behind.
Idempotent retries: if you retry a whole transaction after a serialization failure, the SQL must be safe to run twice or you must detect that the first attempt committed (the network died before you saw COMMIT OK).
Nested transactions: SQLite has SAVEPOINT. Postgres has savepoints. libpqxx exposes them. A second BEGIN on a connection that already has a transaction is an error on most engines. Do not "begin" in a helper if the caller already began.
Read-only transactions (pqxx::read_transaction, BEGIN READ ONLY) let replicas and Postgres optimise. Use them for GET handlers.
Code Examples
#include <sqlite3.h>
#include <stdexcept>
#include <string>
void transfer(sqlite3* db, int from, int to, int cents) {
char* err = nullptr;
if (sqlite3_exec(db, "BEGIN IMMEDIATE;", nullptr, nullptr, &err) != SQLITE_OK)
throw std::runtime_error(err ? err : "begin");
try {
std::string a = "update acct set cents = cents - " + std::to_string(cents)
+ " where id = " + std::to_string(from);
// Demo only: production code binds cents/ids (see prepared-statements).
if (sqlite3_exec(db, a.c_str(), nullptr, nullptr, &err) != SQLITE_OK)
throw std::runtime_error("debit");
std::string b = "update acct set cents = cents + " + std::to_string(cents)
+ " where id = " + std::to_string(to);
if (sqlite3_exec(db, b.c_str(), nullptr, nullptr, &err) != SQLITE_OK)
throw std::runtime_error("credit");
sqlite3_exec(db, "COMMIT;", nullptr, nullptr, nullptr);
} catch (...) {
sqlite3_exec(db, "ROLLBACK;", nullptr, nullptr, nullptr);
throw;
}
}#include <pqxx/pqxx>
void transfer(pqxx::connection& c, int from, int to, int cents) {
pqxx::work txn(c);
txn.exec_params("update acct set cents = cents - $1 where id=$2", cents, from);
txn.exec_params("update acct set cents = cents + $1 where id=$2", cents, to);
txn.commit();
}Common Gotchas
Two statements, no transaction
a crash between them corrupts the business invariant.
The SQLite example concatenates integers
safe for `int`, still the wrong habit. Bind. The libpqxx example is the one to copy.
Destructor rollback
if you `return` before `commit`, the work is undone. That is correct and surprising.
Long transactions
do CPU and HTTP outside `BEGIN`/`COMMIT`.