[php-src] PR #19317: Add PDO_SQLITE_ATTR_TRANSACTION_MODE

From: Date: Wed, 30 Jul 2025 16:08:41 +0000
Subject: [php-src] PR #19317: Add PDO_SQLITE_ATTR_TRANSACTION_MODE
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-32711@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/19317 Author: stancl Resolves #8967 First time contributing to this repo, so please review carefully. Especially the use of zvals and the placement of new constants. This PR lets the user select which transaction mode Pdo\Sqlite should be using. When a transaction mode is not specified, SQLite defaults to DEFERRED transactions which cause issues with locks in concurrent requests. See https://www.sqlite.org/lang_transaction.html When you start a deferred transaction, the lock is only acquired when you actually interact with the database. If you have a transaction that first reads and only then tries to write, it will start by acquiring a shared lock and only when you try to read does it try to upgrade to an exclusive lock. However, this logic of upgrading locks doesn't really respect the busy_timeout pragma, so it just immediately fails with SQLITE_BUSY. On the other hand, when you start an immediate transaction, you don't have to worry about getting SQLITE_BUSY unless another transaction holds a lock for longer than the busy_timeout. So the new transaction just waits for a bit rather than immediately failing. This is the behavior you want in production. If you look at the linked issue, I include a simple reproduction using pcntl_fork() that uses two concurrent processes. If you run the reproduction just as php test.php, one of the transactions fails with SQLITE_BUSY. If you run it as php test.php immediate, the code **does not use $pdo->beginTransaction() and instead uses $pdo->exec('begin immediate transaction')**. With that, the second transaction simply waits for the first one to release the lock. The problem with starting transactions using a custom statement you exec() is that this is simply not an option in many cases, frameworks prefer calling $pdo->beginTransaction(). I've had to solve this in an application of mine by creating a wrapper around the framework's transaction function that first writes to a dummy table and only then executes the callback. That way the transaction always starts with a write lock and doesn't run into the issue of trying to promote a lock. But this doesn't work in every scenario, for instance when the framework's own logic heavily uses transactions that you have no way of customizing with your own wrappers. Not like the wrappers are a proper solution anyway. PDO attributes are a perfect fit for this because you can simply set the attribute and no other logic has to change. Most frameworks let you set these in a config file as well. The only thing I'm not sure about is how to structure these attributes. The transaction mode has 3 possible values. We need to represent those somehow. We could take the transaction mode string but then we'd need to deal with managing the lifetime of that string allocation. We could use boolean attributes like ATTR_TRANSACTION_MODE_DEFERRED => true but then we'd need to handle invalid states like two of these attributes being set at once. For that reason I went with one attribute as the "key" and 3 attributes as the "values". These are just class constants on Pdo\Sqlite. Tests are not included since I'm not sure how this PR should be tested. We could have a basic test that checks the setAttribute()/getAttribute() behavior. However I don't think SQLite has any API that'd give us the transaction mode of the current transaction, and inferring that from some lock contention reproductions like my comment in the issue thread doesn't seem like a good idea.

« previous php.git-pulls (#32711) next »