Reset connection when closing within TransactionScope (#6424) · npgsql/npgsql@792a273 · GitHub
Skip to content

Commit 792a273

Browse files
authored
Reset connection when closing within TransactionScope (#6424)
Fixes #3735
1 parent 0675b3a commit 792a273

5 files changed

Lines changed: 70 additions & 8 deletions

File tree

‎src/Npgsql/Internal/NpgsqlConnector.cs‎

Lines changed: 35 additions & 0 deletions

‎src/Npgsql/NpgsqlConnection.cs‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Data.Common;
66
using System.Diagnostics;
77
using System.Diagnostics.CodeAnalysis;
8-
using System.IO;
98
using System.Net.Security;
109
using System.Net.Sockets;
1110
using System.Runtime.CompilerServices;
@@ -844,14 +843,13 @@ async Task CloseAsync(bool async)
844843

845844
if (EnlistedTransaction != null)
846845
{
847-
// A System.Transactions transaction is still in progress
848-
849-
connector.Connection = null;
850-
851-
// Close the connection and disconnect it from the resource manager but leave the
846+
// A System.Transactions transaction is still in progress.
847+
// Close the connection and disconnect it from the resource manager and reset the connector, but leave the
852848
// connector in an enlisted pending list in the data source. If another connection is opened within
853849
// the same transaction scope, we will reuse this connector to avoid escalating to a distributed
854-
// transaction
850+
// transaction.
851+
connector.ResetWithinEnlistedTransaction();
852+
connector.Connection = null;
855853
_dataSource?.AddPendingEnlistedConnector(connector, EnlistedTransaction);
856854

857855
EnlistedTransaction = null;

‎src/Npgsql/NpgsqlDataSource.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ internal sealed class ReloadableState(NpgsqlDatabaseInfo databaseInfo, PgSeriali
4848
public IDbTypeResolver? DbTypeResolver { get; } = dbTypeResolver;
4949
}
5050

51-
5251
internal TransportSecurityHandler TransportSecurityHandler { get; }
5352

5453
internal Action<SslClientAuthenticationOptions>? SslClientAuthenticationOptionsCallback { get; }

‎test/Npgsql.Tests/MultipleHostsTests.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,8 @@ public async Task Transaction_enlist_reuses_connection(string targetSessionAttri
873873
TargetSessionAttributes = targetSessionAttributes,
874874
ServerCompatibilityMode = ServerCompatibilityMode.NoTypeLoading,
875875
MaxPoolSize = 10,
876+
// Our mock PG server doesn't know how to handle the reset messages
877+
NoResetOnClose = true,
876878
};
877879

878880
using var _ = CreateTempPool(csb, out var connString);

‎test/Npgsql.Tests/SystemTransactionTests.cs‎

Lines changed: 28 additions & 0 deletions

0 commit comments

Comments
 (0)