3 namespace BookStack\Util;
6 use Illuminate\Support\Facades\DB;
10 * Run the given code within a database transactions.
11 * Wraps Laravel's own transaction method, but sets a specific runtime isolation method.
12 * This sets a session level since this won't cause issues if already within a transaction,
13 * and this should apply to the next transactions anyway.
15 * "READ COMMITTED" ensures that changes from other transactions can be read within
16 * a transaction, even if started afterward (and for example, it was blocked by the initial
17 * transaction). This is quite important for things like permission generation, where we would
18 * want to consider the changes made by other committed transactions by the time we come to
19 * regenerate permission access.
22 * @template TReturn of mixed
24 class DatabaseTransaction
27 * @param (Closure(static): TReturn) $callback
29 public function __construct(
30 protected Closure $callback
37 public function run(): mixed
39 DB::statement('SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED');
40 return DB::transaction($this->callback);