Problem/Motivation

A Transaction object that goes out of scope while still active is committed. Deprecated in 11.5 (#3584238); removal is #3607070, which is postponed while what should happen instead is decided.

Until then there is no way to ask for the safe behaviour. If code returns early, throws, or a statement fails (a duplicate key, a string in an integer column), whatever ran before it is committed. The same happens at shutdown: Database::commitAllOnShutdown() commits every open transaction, so exit() halfway through a unit of work commits half of it.

The case that motivated this is hook_update_N(). On PostgreSQL and SQLite, schema changes are transactional, so a run of updates could be all or nothing: one outer transaction, each update's own transaction a savepoint inside it, and a failure anywhere rolls the whole run back. Today the outer transaction commits on the way out of a failure, so a caller can only get that with workarounds (in our case, a deferred foreign key violated by a temporary row, plus a shutdown function that rolls back).

Proposed resolution

Let the caller choose, per transaction, with a backwards-compatible default:

$transaction = $connection->startTransaction('', OnDestruct::Rollback);
// ... work ...
$transaction->commitOrRelease(); // the only way to commit
  • New enum \Drupal\Core\Database\Transaction\OnDestruct: Commit (default, the current, deprecated behaviour) and Rollback.
  • Connection::startTransaction($name = '', OnDestruct $onDestruct = OnDestruct::Commit); TransactionManagerInterface::push() gains the parameter commented out, per the BC policy for interfaces. The value is stored on StackItem.
  • When a Rollback transaction is destructed still active, the transaction manager rolls it back, including any savepoints still open above it (voided first), and runs post-transaction callbacks with success FALSE.
  • commitAll() at shutdown rolls back from the outermost Rollback item instead of committing it, so exit() mid-work rolls back too.
  • If a DDL statement on MySQL already committed a Rollback root (the Voided state), it cannot be rolled back; an E_USER_WARNING says so instead of failing silently.

This does not decide #3607070; it gives code that needs the safe behaviour a way to opt into it now, and a data point for what the default should become.

Remaining tasks

  • Review.
  • Decide whether savepoints opened inside a Rollback root should inherit it. Today a failed inner unit's savepoint (default Commit) is released as the exception unwinds (with the deprecation notice), and the root then rolls everything back, so the outcome is right unless a caller catches the exception and carries on.
  • Change record.

Tests

Kernel tests in TransactionTest, passing on PostgreSQL 18, MariaDB 11.8 and SQLite: destructed uncommitted, explicit commit, PHP exception, database errors (string in an integer column, value too long, duplicate key, NULL in NOT NULL, unknown column; SQLite skips the two it does not enforce), a table created then rolled back (transactional-DDL drivers), savepoints outliving and inside the root, post-transaction callbacks, shutdown for root and savepoint, the MySQL DDL warning, and two released savepoints rolled back by a failing second one.

API changes

New enum OnDestruct; new optional parameter on Connection::startTransaction(); new optional (commented) parameter on TransactionManagerInterface::push(); new public readonly property StackItem::$onDestruct. No behaviour changes unless OnDestruct::Rollback is passed.

Release notes snippet

Database transactions can now be started with OnDestruct::Rollback, so that a transaction that is not explicitly committed is rolled back rather than committed.

Issue fork drupal-3641957

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

abh.ai created an issue. See original summary.

abh.ai’s picture

Issue summary: View changes