Skip to content

FEATURE: Add rollback and use READ COMMITTED on MySQL and MariaDB - #10

Merged
bwaidelich merged 2 commits into
mainfrom
feature/rollback-and-read-committed
Nov 3, 2026
Merged

bwaidelich merged 2 commits into
mainfrom
feature/rollback-and-read-committed

Conversation

@bwaidelich

@bwaidelich bwaidelich commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner

Summary

Implements SubscriptionStore::rollback() from bwaidelich/subscription-engine#14 and stops workers that boot concurrently from deadlocking on MySQL and MariaDB.

 beginTransaction()
   if SQLite
     BEGIN EXCLUSIVE
     return
+  if MySQL or MariaDB and no transaction is active
+    SET TRANSACTION ISOLATION LEVEL READ COMMITTED   # next transaction only
   dbal->beginTransaction()

+rollback()
+  ROLLBACK on SQLite, otherwise dbal->rollBack()

With REPEATABLE READ, SELECT ... FOR UPDATE takes next-key locks on the status index. A worker that activates a subscription moves its row within that index and deadlocks with workers waiting for the lock.

tests/Integration/
├── ConcurrentCatchUpTest.php       # worker processes compete on a real database
├── worker.php                      # one worker process
├── DbalEventStoreAdapter.php       # minimal event store on the same connection
├── StoreWithRepeatableRead.php     # provokes real deadlocks
└── StoreWithoutRowLocks.php        # negative control

The integration tests are skipped unless TEST_DATABASE_DSN is set. A new CI job sets it for MySQL 8.4, MariaDB 11.4 and PostgreSQL 17.

The CI job installs wwwision/subscription-engine:2.x-dev, because the tests rely on limit and rollback(). That step can be dropped once 2.0 is released.

Evidence

  • Before: with 3 workers booting at once on MySQL and MariaDB, concurrentBootsDoNotDeadlock fails with DeadlockException
    After: passes

  • Before: workers that caught the exception and kept going ended with subscriptions in ERROR at about 340–370 of 800 events
    After: workersThatKeepGoingAfterARealDeadlockPersistEverything provokes real deadlocks with REPEATABLE READ, and every event is handled exactly once

Local run Result
MySQL, MariaDB, DBAL 4, 3 repeats OK (34 tests)
PostgreSQL, DBAL 4, 3 repeats 34 tests passing, 2 skipped (MySQL and MariaDB only)
All three, DBAL 3, once each same results
Core 1.4.0, no TEST_DATABASE_DSN 21 unit tests passing, 13 skipped, phpstan clean

Mutants that the tests catch: rollback() as a no-op, no guard for active transactions, SET SESSION instead of the next transaction only.

Merge Danger

Door: two-way until tagged, then one-way for the behavior change

Blast Radius: MySQL

The constraint still allows ^1, so a release also changes the isolation level for users on subscription engine 1.x. On MySQL and MariaDB, subscriber handlers that share the connection now run with READ COMMITTED during a catch-up, so a handler relying on a stable snapshot across statements sees newly committed data. PostgreSQL and SQLite are unchanged, as is any transaction started outside of the store.

With REPEATABLE READ, workers that wait for a subscription lock deadlock
with a worker that changes the status of a subscription, because
SELECT ... FOR UPDATE takes next-key locks on the status index. The level
only applies to the next transaction, not to the session.

rollback() is required by SubscriptionStore of subscription engine 2.0.
Worker processes compete for the same subscriptions on MySQL, MariaDB and
PostgreSQL. The tests are skipped unless TEST_DATABASE_DSN is set, the new
CI job sets it for each database.
@bwaidelich bwaidelich changed the title Feature/rollback and read committed FEATURE: Add rollback and use READ COMMITTED on MySQL and MariaDB Nov 3, 2026
@bwaidelich
bwaidelich merged commit a98387e into main Nov 3, 2026
8 checks passed
@bwaidelich
bwaidelich deleted the feature/rollback-and-read-committed branch November 3, 2026 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant