begin_work(), commit_work() and rollback_work()

book ok;

conn->begin_work();

// ...

    ok=false;

    conn->begin_work();

// ...

    ok=true;

// ...

    if (ok)
        conn->commit_work();
    else
        conn->rollback__work();

// ...

if (ok)
    conn->commit_work();
else
    conn->rollback_work();

autocommit(), commit(), and rollback() should be used only when the underlying database driver does not implement modern START TRANSACTION and SAVEPOINT SQL that allows for nested logical transactions.

begin_work() opens a logical transaction until it's closed by commit_work() or rollback_work(). While a transaction is open, invoking begin_work() again opens a nested transaction.

The next call to commit_work() or rollback_work() closes the nested transaction. The nested transaction's changes get either commited to the original transaction, or undone, leaving the original transaction in its state prior to the nested transaction's start. The original transaction remains open until its own matching commit_work() or rollback_work().

Nested transactions get implemented using SQL savepoints. The limit on the maximum number of nested transactions depends on the database driver-specific on the maximum number of savepoints within a transaction.