Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispose closed pool connections #260

Merged
merged 5 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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