@@ -51,6 +51,9 @@ abstract class SqlCommonConnectionPool implements SqlConnectionPool
5151
5252 private readonly DeferredFuture $ onClose ;
5353
54+ /** @var \WeakMap<TStatement, true> */
55+ private \WeakMap $ statements ;
56+
5457 /**
5558 * Creates a Statement of the appropriate type using the Statement object returned by the Link object and the
5659 * given release callable.
@@ -114,6 +117,10 @@ public function __construct(
114117 }
115118
116119 $ this ->connections = $ connections = new \SplObjectStorage ();
120+
121+ /** @var \WeakMap<TStatement, true> For Psalm. */
122+ $ this ->statements = new \WeakMap ();
123+
117124 $ this ->idle = $ idle = new \SplQueue ();
118125 $ this ->onClose = new DeferredFuture ();
119126
@@ -207,6 +214,10 @@ public function close(): void
207214 async (fn () => $ connection ->close ())->ignore ();
208215 }
209216
217+ foreach ($ this ->statements as $ statement => $ _ ) {
218+ $ statement ->close ();
219+ }
220+
210221 $ this ->onClose ->complete ();
211222
212223 $ this ->awaitingConnection ?->error(new SqlException ("Connection pool closed " ));
@@ -359,7 +370,12 @@ public function execute(string $sql, array $params = []): SqlResult
359370 public function prepare (string $ sql ): SqlStatement
360371 {
361372 /** @psalm-suppress InvalidArgument Psalm is not properly detecting the templated return type. */
362- return $ this ->createStatementPool ($ sql , $ this ->prepareStatement (...));
373+ $ statement = $ this ->createStatementPool ($ sql , $ this ->prepareStatement (...));
374+
375+ $ this ->statements [$ statement ] = true ;
376+
377+ /** @var TStatement $statement Psalm is not properly detecting the templated type. */
378+ return $ statement ;
363379 }
364380
365381 /**
0 commit comments