Skip to content
Open
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
29 changes: 29 additions & 0 deletions src/Npgsql/Internal/NpgsqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2619,8 +2619,14 @@ void Cleanup()
_certificates = null;
}

[MemberNotNull(nameof(_resetWithoutDeallocateMessage))]
void GenerateResetMessage()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roji do you think it might make sense to move _resetWithoutDeallocateMessage to the data source/database info, rather than cache per each connector? Not in this pr of course.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea, makes sense.

{
// Generate a reset message that resets connection state without using DISCARD ALL.
// This is used in two scenarios:
// 1. When closing a pooled connection that has prepared statements (DISCARD ALL would deallocate them)
// 2. When closing a connection within an enlisted System.Transactions transaction (DISCARD ALL cannot
// run inside a transaction block, but its component commands can)
var sb = new StringBuilder("SET SESSION AUTHORIZATION DEFAULT;RESET ALL;");
_resetWithoutDeallocateResponseCount = 2;
if (DatabaseInfo.SupportsCloseAll)
Expand Down Expand Up @@ -2728,6 +2734,29 @@ internal async Task Reset(bool async)
}
}

/// <summary>
/// Called when a pooled connection with an enlisted System.Transactions transaction is closed.
/// Since we're inside a transaction block, we cannot send DISCARD ALL, DISCARD TEMP or DISCARD SEQUENCES;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment says DISCARD TEMP and DISCRD SEQUENCES cannot be used, but I can see they are being added inside GenerateResetMessage() which is called below.

This seems to contradict the comment, or am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is wrong (AI-generated), I'll fix - DISCARD TEMP and DISCARD SEQUENCES seem to work fine within transactions (but DISCARD ALL is indeed disallowed).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine that's to make sure that in case they add a new command in the future which can't run in transactions, that way they'll not break anyone.

/// we prepend a reset message that only includes commands that can safely run within a transaction.
/// </summary>
internal void ResetWithinEnlistedTransaction()
{
// Our buffer may contain unsent prepended messages, so clear it out.
WriteBuffer.Clear();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we start user action here to prevent messing up concurrent keepalive?

PendingPrependedResponses = 0;

ResetReadBuffer();

if (_resetWithoutDeallocateMessage is null)
{
GenerateResetMessage();
}

PrependInternalMessage(_resetWithoutDeallocateMessage, _resetWithoutDeallocateResponseCount);

DataReader.UnbindIfNecessary();
}

/// <summary>
/// The connector may have allocated an oversize read buffer, to hold big rows in non-sequential reading.
/// This switches us back to the original one and returns the buffer to <see cref="ArrayPool{T}" />.
Expand Down
11 changes: 5 additions & 6 deletions src/Npgsql/NpgsqlConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -844,14 +844,13 @@ async Task CloseAsync(bool async)

if (EnlistedTransaction != null)
{
// A System.Transactions transaction is still in progress

connector.Connection = null;

// Close the connection and disconnect it from the resource manager but leave the
// A System.Transactions transaction is still in progress.
// Close the connection and disconnect it from the resource manager and reset the connector, but leave the
// connector in an enlisted pending list in the data source. If another connection is opened within
// the same transaction scope, we will reuse this connector to avoid escalating to a distributed
// transaction
// transaction.
connector.ResetWithinEnlistedTransaction();
connector.Connection = null;
_dataSource?.AddPendingEnlistedConnector(connector, EnlistedTransaction);

EnlistedTransaction = null;
Expand Down
2 changes: 2 additions & 0 deletions test/Npgsql.Tests/MultipleHostsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,8 @@
TargetSessionAttributes = targetSessionAttributes,
ServerCompatibilityMode = ServerCompatibilityMode.NoTypeLoading,
MaxPoolSize = 10,
// Our mock PG server doesn't know how to handle the reset messages
NoResetOnClose = true,
};

using var _ = CreateTempPool(csb, out var connString);
Expand Down Expand Up @@ -911,7 +913,7 @@
.WriteCommandComplete()
.WriteReadyForQuery()
.FlushAsync();
scope.Complete();

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 14, Release, net10.0)

Transaction_enlist_reuses_connection("read-only"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 14, Release, net10.0)

Transaction_enlist_reuses_connection("read-write"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 14, Release, net10.0)

Transaction_enlist_reuses_connection("prefer-standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 14, Release, net10.0)

Transaction_enlist_reuses_connection("prefer-primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 14, Release, net10.0)

Transaction_enlist_reuses_connection("standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 14, Release, net10.0)

Transaction_enlist_reuses_connection("primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 14, Release, net10.0)

Transaction_enlist_reuses_connection("any"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 15, Release, net10.0)

Transaction_enlist_reuses_connection("read-only"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 15, Release, net10.0)

Transaction_enlist_reuses_connection("read-write"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 15, Release, net10.0)

Transaction_enlist_reuses_connection("prefer-standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 15, Release, net10.0)

Transaction_enlist_reuses_connection("prefer-primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 15, Release, net10.0)

Transaction_enlist_reuses_connection("standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 15, Release, net10.0)

Transaction_enlist_reuses_connection("primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 15, Release, net10.0)

Transaction_enlist_reuses_connection("any"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 16, Release, net10.0)

Transaction_enlist_reuses_connection("read-only"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 16, Release, net10.0)

Transaction_enlist_reuses_connection("prefer-standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 16, Release, net10.0)

Transaction_enlist_reuses_connection("read-write"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 16, Release, net10.0)

Transaction_enlist_reuses_connection("prefer-primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 16, Release, net10.0)

Transaction_enlist_reuses_connection("standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 16, Release, net10.0)

Transaction_enlist_reuses_connection("primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 16, Release, net10.0)

Transaction_enlist_reuses_connection("any"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 18, Debug, net10.0)

Transaction_enlist_reuses_connection("read-only"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 18, Debug, net10.0)

Transaction_enlist_reuses_connection("read-write"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 18, Debug, net10.0)

Transaction_enlist_reuses_connection("prefer-standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 18, Debug, net10.0)

Transaction_enlist_reuses_connection("prefer-primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 18, Debug, net10.0)

Transaction_enlist_reuses_connection("standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 18, Debug, net10.0)

Transaction_enlist_reuses_connection("primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 18, Debug, net10.0)

Transaction_enlist_reuses_connection("any"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 18, Release, net10.0)

Transaction_enlist_reuses_connection("read-only"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 18, Release, net10.0)

Transaction_enlist_reuses_connection("read-write"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 18, Release, net10.0)

Transaction_enlist_reuses_connection("prefer-standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 18, Release, net10.0)

Transaction_enlist_reuses_connection("prefer-primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 18, Release, net10.0)

Transaction_enlist_reuses_connection("standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 18, Release, net10.0)

Transaction_enlist_reuses_connection("primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 18, Release, net10.0)

Transaction_enlist_reuses_connection("any"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 17, Release, net10.0)

Transaction_enlist_reuses_connection("read-only"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 17, Release, net10.0)

Transaction_enlist_reuses_connection("read-write"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 17, Release, net10.0)

Transaction_enlist_reuses_connection("prefer-standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 17, Release, net10.0)

Transaction_enlist_reuses_connection("prefer-primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 17, Release, net10.0)

Transaction_enlist_reuses_connection("standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 17, Release, net10.0)

Transaction_enlist_reuses_connection("primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, 17, Release, net10.0)

Transaction_enlist_reuses_connection("any"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-15, 16, Release, net10.0)

Transaction_enlist_reuses_connection("read-only"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-15, 16, Release, net10.0)

Transaction_enlist_reuses_connection("read-write"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-15, 16, Release, net10.0)

Transaction_enlist_reuses_connection("prefer-standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-15, 16, Release, net10.0)

Transaction_enlist_reuses_connection("prefer-primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-15, 16, Release, net10.0)

Transaction_enlist_reuses_connection("standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-15, 16, Release, net10.0)

Transaction_enlist_reuses_connection("primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-15, 16, Release, net10.0)

Transaction_enlist_reuses_connection("any"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, 18, Release, net10.0)

Transaction_enlist_reuses_connection("read-only"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, 18, Release, net10.0)

Transaction_enlist_reuses_connection("prefer-standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, 18, Release, net10.0)

Transaction_enlist_reuses_connection("read-write"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, 18, Release, net10.0)

Transaction_enlist_reuses_connection("prefer-primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, 18, Release, net10.0)

Transaction_enlist_reuses_connection("standby"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, 18, Release, net10.0)

Transaction_enlist_reuses_connection("primary"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

Check failure on line 916 in test/Npgsql.Tests/MultipleHostsTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022, 18, Release, net10.0)

Transaction_enlist_reuses_connection("any"

Npgsql.NpgsqlException : Exception while reading from stream ----> System.TimeoutException : Timeout during reading attempt

async Task Query(string connectionString)
{
Expand Down
28 changes: 28 additions & 0 deletions test/Npgsql.Tests/SystemTransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,34 @@ public void Reuse_connection_rollback()
AssertNumberOfRows(0, tableName);
}

[Test, IssueLink("https://github.com/npgsql/npgsql/issues/3735")]
public void Reuse_connection_resets_temp_tables()
{
// When a connection is closed inside a TransactionScope and then reopened,
// temp tables should be discarded.
using var dataSource = CreateDataSource(csb => csb.Enlist = true);
using (new TransactionScope())
using (var conn = dataSource.CreateConnection())
{
conn.Open();
var processId = conn.ProcessID;

// Create a temp table
conn.ExecuteNonQuery("CREATE TEMP TABLE temp_test (id INT)");

conn.Close();

// Reopen - should get the same physical connection but with reset state
conn.Open();
Assert.That(conn.ProcessID, Is.EqualTo(processId), "Should reuse the same physical connection");

// The temp table should have been discarded
Assert.That(() => conn.ExecuteScalar("SELECT COUNT(*) FROM temp_test"),
Throws.Exception.TypeOf<PostgresException>()
.With.Property(nameof(PostgresException.SqlState)).EqualTo(PostgresErrorCodes.UndefinedTable));
}
}

[Test, Ignore("Timeout doesn't seem to fire on .NET Core / Linux")]
public void Timeout_triggers_rollback_while_busy()
{
Expand Down
Loading