Skip to content

Commit ce4faf1

Browse files
Add ability to specify a transaction mode for SQLite connection (#56681)
* Add ability to specify a transaction mode for SQLite connection * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 450a04e commit ce4faf1

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

config/database.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
'busy_timeout' => null,
4242
'journal_mode' => null,
4343
'synchronous' => null,
44+
'transaction_mode' => 'DEFERRED',
4445
],
4546

4647
'mysql' => [

src/Illuminate/Database/Concerns/ManagesTransactions.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
namespace Illuminate\Database\Concerns;
44

55
use Closure;
6+
use Illuminate\Database\Connection;
67
use Illuminate\Database\DeadlockException;
78
use RuntimeException;
89
use Throwable;
910

11+
/**
12+
* @mixin Connection
13+
*/
1014
trait ManagesTransactions
1115
{
1216
/**
@@ -148,7 +152,7 @@ protected function createTransaction()
148152
$this->reconnectIfMissingConnection();
149153

150154
try {
151-
$this->getPdo()->beginTransaction();
155+
$this->executeBeginTransactionStatement();
152156
} catch (Throwable $e) {
153157
$this->handleBeginTransactionException($e);
154158
}
@@ -184,7 +188,7 @@ protected function handleBeginTransactionException(Throwable $e)
184188
if ($this->causedByLostConnection($e)) {
185189
$this->reconnect();
186190

187-
$this->getPdo()->beginTransaction();
191+
$this->executeBeginTransactionStatement();
188192
} else {
189193
throw $e;
190194
}

src/Illuminate/Database/Connection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,16 @@ public function unsetEventDispatcher()
14701470
$this->events = null;
14711471
}
14721472

1473+
/**
1474+
* Run the statement to start a new transaction.
1475+
*
1476+
* @return void
1477+
*/
1478+
protected function executeBeginTransactionStatement()
1479+
{
1480+
$this->getPdo()->beginTransaction();
1481+
}
1482+
14731483
/**
14741484
* Set the transaction manager instance on the connection.
14751485
*

src/Illuminate/Database/SQLiteConnection.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ public function getDriverTitle()
2020
return 'SQLite';
2121
}
2222

23+
/**
24+
* Run the statement to start a new transaction.
25+
*
26+
* @return void
27+
*/
28+
protected function executeBeginTransactionStatement()
29+
{
30+
if (version_compare(PHP_VERSION, '8.4.0') >= 0) {
31+
$mode = $this->getConfig('transaction_mode') ?? 'DEFERRED';
32+
33+
$this->getPdo()->exec("BEGIN {$mode} TRANSACTION");
34+
35+
return;
36+
}
37+
38+
$this->getPdo()->beginTransaction();
39+
}
40+
2341
/**
2442
* Escape a binary value for safe SQL embedding.
2543
*

0 commit comments

Comments
 (0)