Skip to content

Commit

Permalink
Unbind the transaction for unpooled connections (#3689)
Browse files Browse the repository at this point in the history
Fixes #3686
  • Loading branch information
vonzshik committed Apr 29, 2021
1 parent ea77317 commit 17cdb2e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Npgsql/NpgsqlConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,11 @@ async Task CloseAsync(CancellationToken cancellationToken)
else
{
if (_pool == null)
{
// We're already doing the same in the NpgsqlConnector.Reset for pooled connections
connector.Transaction?.UnbindIfNecessary();
connector.Close();
}
else
{
// Clear the buffer, roll back any pending transaction and prepend a reset message if needed
Expand Down
23 changes: 23 additions & 0 deletions test/Npgsql.Tests/TransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,29 @@ public async Task AccessConnectionOnCompletedTransaction()
Assert.That(tx.Connection, Is.SameAs(conn));
}


[Test, IssueLink("https://github.com/npgsql/npgsql/issues/3686")]
public async Task Bug3686()
{
if (IsMultiplexing)
return;

var csb = new NpgsqlConnectionStringBuilder(ConnectionString)
{
Pooling = false
};

await using var conn = await OpenConnectionAsync(csb);
await using var tx = await conn.BeginTransactionAsync();
await conn.ExecuteNonQueryAsync("SELECT 1", tx);
await tx.CommitAsync();
await conn.CloseAsync();
Assert.DoesNotThrow(() =>
{
_ = tx.Connection;
});
}

class NoTransactionDatabaseInfoFactory : INpgsqlDatabaseInfoFactory
{
public async Task<NpgsqlDatabaseInfo?> Load(NpgsqlConnection conn, NpgsqlTimeout timeout, bool async)
Expand Down

0 comments on commit 17cdb2e

Please sign in to comment.