Skip to content

Commit 9c2a1bb

Browse files
committed
Close pgsql connection in destructor
1 parent ce82b06 commit 9c2a1bb

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ parameters:
149149
- '~^Parameter #1 \$row of method Doctrine\\DBAL\\Driver\\PgSQL\\Result\:\:mapNumericRow\(\) expects array<int, string\|null>, array<int\|string, string\|null> given\.$~'
150150

151151
# Ignore isset() checks in destructors.
152+
- '~^Property Doctrine\\DBAL\\Driver\\PgSQL\\Connection\:\:\$connection \(PgSql\\Connection\|resource\) in isset\(\) is not nullable\.$~'
152153
- '~^Property Doctrine\\DBAL\\Driver\\PgSQL\\Statement\:\:\$connection \(PgSql\\Connection\|resource\) in isset\(\) is not nullable\.$~'
153154

154155
# On PHP 7.4, pg_fetch_all() might return false for empty result sets.

psalm.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@
645645
<errorLevel type="suppress">
646646
<!-- PgSql objects are represented as resources in PHP 7.4 -->
647647
<referencedFunction name="pg_affected_rows"/>
648+
<referencedFunction name="pg_close"/>
648649
<referencedFunction name="pg_escape_bytea"/>
649650
<referencedFunction name="pg_escape_identifier"/>
650651
<referencedFunction name="pg_escape_literal"/>
@@ -752,6 +753,7 @@
752753
<directory name="tests"/>
753754

754755
<!-- Ignore isset() checks in destructors. -->
756+
<file name="src/Driver/PgSQL/Connection.php"/>
755757
<file name="src/Driver/PgSQL/Statement.php"/>
756758
</errorLevel>
757759
</RedundantPropertyInitializationCheck>

src/Driver/PgSQL/Connection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function gettype;
1515
use function is_object;
1616
use function is_resource;
17+
use function pg_close;
1718
use function pg_escape_bytea;
1819
use function pg_escape_literal;
1920
use function pg_get_result;
@@ -47,6 +48,15 @@ public function __construct($connection)
4748
$this->parser = new Parser(false);
4849
}
4950

51+
public function __destruct()
52+
{
53+
if (! isset($this->connection)) {
54+
return;
55+
}
56+
57+
@pg_close($this->connection);
58+
}
59+
5060
public function prepare(string $sql): Statement
5161
{
5262
$visitor = new ConvertParameters();

0 commit comments

Comments
 (0)