Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Adding `JsonbListView` with `isSqlNull(int index)` method to check if a JSONB array has SQL or JSON null value.
- Fix double-timezone encoding bug.
- Fix statement scope bug.

## 3.5.10

Expand Down
10 changes: 7 additions & 3 deletions lib/src/v3/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -705,13 +705,17 @@ class _PreparedStatement extends Statement {

_PreparedStatement(this._description, this._name, this._session, this._trace);

_PgSessionBase get _effectiveSession =>
_session._connection._activeTransaction ?? _session;

@override
ResultStream bind(Object? parameters) {
return _BoundStatement(
this,
_description.bindParameters(
parameters,
ignoreSuperfluous: _session._settings.ignoreSuperfluousParameters,
ignoreSuperfluous:
_effectiveSession._settings.ignoreSuperfluousParameters,
),
);
}
Expand Down Expand Up @@ -758,7 +762,7 @@ class _PreparedStatement extends Statement {
final list = _portalsToClose;
while (list != null && list.isNotEmpty) {
final portalName = list.removeFirst();
await _session._sendAndWaitForQuery<CloseCompleteMessage>(
await _effectiveSession._sendAndWaitForQuery<CloseCompleteMessage>(
CloseMessage.portal(portalName),
stackTrace: stackTrace,
);
Expand Down Expand Up @@ -825,7 +829,7 @@ class _PgResultStreamSubscription
this._controller,
this._source, {
Trace? callerTrace,
}) : session = statement.statement._session,
}) : session = statement.statement._effectiveSession,
ignoreRows = false,
_boundStatement = statement,
_parentTrace = statement.statement._trace,
Expand Down
33 changes: 33 additions & 0 deletions test/transaction_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -803,5 +803,38 @@ void main() {
throwsA(isA<UniqueViolationException>()),
);
});

test(
'prepared statement created outside transaction can be used inside it',
() async {
await conn.execute('INSERT INTO t (id) VALUES (1), (2), (3)');

// Prepare outside the transaction.
final stmt = await conn.prepare(
Sql(r'SELECT id FROM t WHERE id = $1', types: [Type.integer]),
);

try {
final result = await conn.runTx((tx) async {
// Use the pre-prepared statement inside the transaction.
// Before the fix this would hang indefinitely because stmt tried to
// acquire the connection's _operationLock, which runTx already holds.
return await stmt.run([2]);
});

expect(result, [
[2],
]);

// Statement still works after the transaction.
final afterTx = await stmt.run([3]);
expect(afterTx, [
[3],
]);
} finally {
await stmt.dispose();
}
},
);
});
}
Loading