CoolCMS API

AtomicTransactionTrait

Real database transaction for Api operations that change several rows, where a partial write would leave an inconsistent state.

Generalizes FeedManager::inRebuildTransaction() as a shared trait.

IMPORTANT: it opens its own transaction only when none is running. A nested call runs in the caller's transaction and only returns the result, so the caller decides about commit and rollback. Inside a TransactionManager transaction a nested rollback could not undo just the inner part anyway: it uses no savepoints, and TransactionManager::rollback() on a nested level only marks the whole transaction rollback-only, so the caller's later commit would roll everything back and throw RollbackOnlyException. Detection goes through the DBAL connection (isTransactionActive()), the only common denominator of both paths, TransactionManager and a direct beginTransaction().

Table of Contents

Methods

inAtomicTransaction()  : Result
Runs `$work` in a transaction: a failed `Result` or an exception means rollback, success means commit.

Methods

inAtomicTransaction()

Runs `$work` in a transaction: a failed `Result` or an exception means rollback, success means commit.

protected inAtomicTransaction(Connection $connection, callable(): Result $work[, string $logChannel = 'api.transaction' ]) : Result

An exception is always logged (otherwise a nested run would lose it without a trace) and converted to Result::failure(DB_ERROR) so the caller gets a single Result.

Parameters
$connection : Connection
$work : callable(): Result
$logChannel : string = 'api.transaction'
Return values
Result
On this page

Search results