Skip to content

Core\Db\Isolation

How much of other transactions' unfinished work your own transaction is allowed to see.

A database runs many transactions at the same time. The isolation level says what your transaction sees of the others while they are still running. The five cases go from ReadUncommitted to Serializable. With ReadUncommitted, a statement may read a row that nobody has committed yet. With Serializable, the result is the same as if the transactions ran one after another.

You pass a case as the isolation option of runTransaction. Most programs leave it out. The transaction then uses the level the database is configured with.

Good to know: the order of the cases is not a ranking. Databases do not agree on where Snapshot belongs, so it is not stronger or weaker than the cases next to it. Pick the case that describes the work you need. A database that does not support the level you choose throws an error. It does not run your work at a weaker level.

Examples

What each level lets you see

<?nvs
// The five isolation levels a transaction can run at. Each line says what your
// statements can see of other transactions that run at the same time.
class Level
{
public static function promise(Core\Db\Isolation $level): string
{
return match ($level) {
Core\Db\Isolation::ReadUncommitted => 'ReadUncommitted: a statement may read rows another transaction has written and not committed',
Core\Db\Isolation::ReadCommitted => 'ReadCommitted: each statement sees what was committed before that statement began',
Core\Db\Isolation::RepeatableRead => 'RepeatableRead: every statement in the transaction sees the same committed rows',
Core\Db\Isolation::Snapshot => 'Snapshot: you read the database as it stood when the transaction began, and clashing writes fail rather than wait',
Core\Db\Isolation::Serializable => 'Serializable: the outcome matches some order in which the transactions ran one at a time',
};
}
}
array<Core\Db\Isolation> $levels = [
Core\Db\Isolation::ReadUncommitted,
Core\Db\Isolation::ReadCommitted,
Core\Db\Isolation::RepeatableRead,
Core\Db\Isolation::Snapshot,
Core\Db\Isolation::Serializable,
];
foreach ($levels as Core\Db\Isolation $level) {
echo Level::promise($level), "\n";
}
Output
ReadUncommitted: a statement may read rows another transaction has written and not committed
ReadCommitted: each statement sees what was committed before that statement began
RepeatableRead: every statement in the transaction sees the same committed rows
Snapshot: you read the database as it stood when the transaction began, and clashing writes fail rather than wait
Serializable: the outcome matches some order in which the transactions ran one at a time

Choosing the level for the job

<?nvs
// `Work::levelFor` picks an isolation level for each kind of work. The choice
// depends on what could go wrong if another transaction changed the same data.
// You pass the result as the `isolation` option of a transaction.
class Work
{
public static function levelFor(string $job): Core\Db\Isolation
{
return match ($job) {
// A transfer changes two accounts. No other transaction may
// change them between the two updates.
'moving money between accounts' => Core\Db\Isolation::Serializable,
// A long report reads many tables. Every statement in it must
// see the same data.
'the month-end report' => Core\Db\Isolation::RepeatableRead,
// Other work only needs to read data that is committed.
default => Core\Db\Isolation::ReadCommitted,
};
}
public static function name(Core\Db\Isolation $level): string
{
return match ($level) {
Core\Db\Isolation::ReadUncommitted => 'ReadUncommitted',
Core\Db\Isolation::ReadCommitted => 'ReadCommitted',
Core\Db\Isolation::RepeatableRead => 'RepeatableRead',
Core\Db\Isolation::Snapshot => 'Snapshot',
Core\Db\Isolation::Serializable => 'Serializable',
};
}
}
array<string> $jobs = [
'moving money between accounts',
'the month-end report',
"counting today's orders",
];
foreach ($jobs as string $job) {
echo $job, ' -> ', Work::name(Work::levelFor($job)), "\n";
}
Output
moving money between accounts -> Serializable
the month-end report -> RepeatableRead
counting today's orders -> ReadCommitted

Leaving the level to the database

<?nvs
// If you do not pass an isolation level, the transaction uses the database's
// default level. Pass a level only for work that needs a stricter one.
class Checkout
{
// `levelFor` returns `null` for a step that uses the default level.
// This is the same as not passing the `isolation` option.
public static function levelFor(string $step): ?Core\Db\Isolation
{
return match ($step) {
// This step reads the stock and writes the order. It must not sell
// the last item twice, so it needs `Serializable`.
'take the payment and reserve the stock' => Core\Db\Isolation::Serializable,
default => null,
};
}
}
array<string> $steps = [
'write the basket to the database',
'take the payment and reserve the stock',
'send the confirmation email',
];
foreach ($steps as string $step) {
var $level = Checkout::levelFor($step);
if ($level == null) {
echo $step, ": whatever the database is set to\n";
} else {
// Only one step returns a level here, and that level is `Serializable`.
// The program names it directly.
echo $step, ": Serializable, and nothing weaker will do\n";
}
}
Output
write the basket to the database: whatever the database is set to
take the payment and reserve the stock: Serializable, and nothing weaker will do
send the confirmation email: whatever the database is set to