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

Fix: do not close connection after an empty statement #403

Merged
merged 1 commit into from
Dec 21, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.4.6

- Fix: do not close connection after an empty statement.

## 3.4.5

- `close(force: true)` to indicate intent to force-close pending queries and resources. [#396](https://github.com/isoos/postgresql-dart/pull/396) by [davidmartos96](https://github.com/davidmartos96)
Expand Down
1 change: 1 addition & 0 deletions lib/src/message_window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Map<int, _ServerMessageFn> _messageTypeMap = {
67: (r, _) => CommandCompleteMessage.parse(r),
68: (r, _) => DataRowMessage.parse(r),
69: ErrorResponseMessage.parse,
73: (_, __) => EmptyQueryResponseMessage(),
75: (r, _) => BackendKeyMessage.parse(r),
82: AuthenticationMessage.parse,
83: (r, l) => ParameterStatusMessage.parse(r),
Expand Down
10 changes: 10 additions & 0 deletions lib/src/messages/server_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,13 @@ Future<XLogDataMessage> parseXLogDataMessage(
}
}

class EmptyQueryResponseMessage extends ServerMessage {
EmptyQueryResponseMessage();

@override
String toString() => 'EmptyQueryResponseMessage()';
}

class UnknownMessage extends ServerMessage {
final int code;
final Uint8List bytes;
Expand Down Expand Up @@ -476,6 +483,9 @@ class UnknownMessage extends ServerMessage {
}
return true;
}

@override
String toString() => 'UnknownMessage($code, length=${bytes.length})';
}

abstract class ErrorFieldId {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/v3/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,8 @@ class _PgResultStreamSubscription
// TODO(osaxma): Prevent executing queries when Streaming Replication
// is ongoing
await _completeQuery();
case EmptyQueryResponseMessage():
break;
default:
// Unexpected message - either a severe bug in this package or in the
// connection. We better close it.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: postgres
description: PostgreSQL database driver. Supports statement reuse and binary protocol and connection pooling.
version: 3.4.5
version: 3.4.6
homepage: https://github.com/isoos/postgresql-dart
topics:
- sql
Expand Down
5 changes: 3 additions & 2 deletions test/pool_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ void main() {
),
);

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

Expand Down
7 changes: 7 additions & 0 deletions test/v3_close_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ void main() {
await conn2.execute(
'select pg_terminate_backend($conn1PID) from pg_stat_activity;');
});

test('empty query does not close connection', () async {
await conn1.execute('-- test');
expect(await conn1.execute('SELECT 1'), [
[1]
]);
});
});

group('force close', () {
Expand Down
Loading