Skip to content

Commit

Permalink
Dispose closed pool connections (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
nehzata authored Nov 26, 2023
1 parent 4240b5d commit b4d97fb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.0.2

- Fix: Dispose disconnected pool `Connection`s. ([#260](https://github.com/isoos/postgresql-dart/pull/260) by [nehzata](https://github.com/nehzata)).

## 3.0.1

- Fix: do not allow `execute` after closing the `Connection`.
Expand Down Expand Up @@ -125,7 +129,7 @@ who helped to push forward.
- Add LSN type and time conversion to and from ms-since-Y2K. [#53](https://github.com/isoos/postgresql-dart/pull/53) by [osaxma](https://github.com/osaxma)
- Fix affected rows parsing in CommandCompleteMessage. [#52](https://github.com/isoos/postgresql-dart/pull/52) by [osaxma](https://github.com/osaxma)
- Introduced new APIs to `PostgreSQLConnection`: `addMessage` to send client messages, `messages` stream to listen to server messages & `useSimpleQueryProtocol` option in `query` method. [#51](https://github.com/isoos/postgresql-dart/pull/51) by [osaxma](https://github.com/osaxma)

## 2.4.6

- Fix crash when manually issuing a transaction statement like `BEGIN` without
Expand Down
16 changes: 9 additions & 7 deletions lib/src/pool/pool_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,18 @@ class PoolImplementation<L> implements Pool<L> {
} finally {
resource.release();
sw.stop();
connection?._elapsedInUse += sw.elapsed;

// If the pool has been closed, this connection needs to be closed as
// well.
if (_semaphore.isClosed || !reuse) {
await connection?._dispose();
} else {
// Allow the connection to be re-used later.
connection?._isInUse = false;
connection?._lastReturned = DateTime.now();
if (connection != null) {
connection._elapsedInUse += sw.elapsed;
if (_semaphore.isClosed || !reuse || !connection.isOpen) {
await connection._dispose();
} else {
// Allow the connection to be re-used later.
connection._isInUse = false;
connection._lastReturned = DateTime.now();
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/pool_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ void main() {

await db.execute('SELECT 1');
});

test('empty query does not lock up pool instance', () async {
final db = Pool.withEndpoints(
[await server.endpoint()],
settings: PoolSettings(
maxConnectionCount: 1,
),
);

await db.execute('-- test'); // this doesn't throw but it causes the connection to close
await db.execute('SELECT 1');
});
});

withPostgresServer('limit pool connections', (server) {
Expand Down

0 comments on commit b4d97fb

Please sign in to comment.