Skip to content

Commit ce9106a

Browse files
committed
Cache statements used during transaction
Prevents de-allocation until after the transaction is ended.
1 parent 06f2c67 commit ce9106a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/Internal/PostgresConnectionTransaction.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,23 @@ final class PostgresConnectionTransaction implements PostgresTransaction
2222

2323
private readonly DeferredFuture $onClose;
2424

25+
/** @var array<int, PostgresStatement> Reference statements so de-allocation occurs after commit/rollback. */
26+
private array $statements = [];
27+
2528
/**
2629
* @param \Closure():void $release
27-
*
28-
* @throws \Error If the isolation level is invalid.
2930
*/
3031
public function __construct(
3132
private readonly PostgresHandle $handle,
3233
\Closure $release,
3334
private readonly TransactionIsolation $isolation
3435
) {
35-
$refCount =& $this->refCount;
36-
$this->release = static function () use (&$refCount, $release): void {
36+
$refCount = &$this->refCount;
37+
$statements = &$this->statements;
38+
$this->release = static function () use (&$refCount, &$statements, $release): void {
3739
if (--$refCount === 0) {
3840
$release();
41+
$statements = [];
3942
}
4043
};
4144

@@ -143,6 +146,8 @@ public function prepare(string $sql): PostgresStatement
143146
throw $exception;
144147
}
145148

149+
$this->statements[\spl_object_id($statement)] ??= $statement;
150+
146151
return new PostgresPooledStatement($statement, $this->release);
147152
}
148153

0 commit comments

Comments
 (0)