Skip to content

Commit 4d7b6ea

Browse files
committed
Add ability to specify a transaction mode for SQLite connection
1 parent 7796b9b commit 4d7b6ea

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-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
@@ -1493,6 +1493,16 @@ public function unsetTransactionManager()
14931493
$this->transactionsManager = null;
14941494
}
14951495

1496+
/**
1497+
* Run the statement to start a new transaction.
1498+
*
1499+
* @return void
1500+
*/
1501+
public function executeBeginTransactionStatement()
1502+
{
1503+
$this->getPdo()->beginTransaction();
1504+
}
1505+
14961506
/**
14971507
* Determine if the connection is in a "dry run".
14981508
*

src/Illuminate/Database/SQLiteConnection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,17 @@ protected function getDefaultPostProcessor()
100100
{
101101
return new SQLiteProcessor;
102102
}
103+
104+
public function executeBeginTransactionStatement()
105+
{
106+
if (version_compare(PHP_VERSION, '8.4.0')) {
107+
$mode = $this->getConfig('transaction_mode') ?? 'DEFERRED';
108+
109+
$this->getPdo()->exec("BEGIN $mode TRANSACTION");
110+
111+
return;
112+
}
113+
114+
$this->getPdo()->beginTransaction();
115+
}
103116
}

0 commit comments

Comments
 (0)