diff --git a/src/Npgsql/MultiplexingDataSource.cs b/src/Npgsql/MultiplexingDataSource.cs index 2eb1763c3c..137b60832a 100644 --- a/src/Npgsql/MultiplexingDataSource.cs +++ b/src/Npgsql/MultiplexingDataSource.cs @@ -98,7 +98,7 @@ async Task MultiplexingWriteLoop() } connector = await OpenNewConnector( - command.Connection!, + command.InternalConnection!, new NpgsqlTimeout(TimeSpan.FromSeconds(Settings.Timeout)), async: true, CancellationToken.None); diff --git a/test/Npgsql.Tests/DataSourceTests.cs b/test/Npgsql.Tests/DataSourceTests.cs index 10565d4615..7ad117c362 100644 --- a/test/Npgsql.Tests/DataSourceTests.cs +++ b/test/Npgsql.Tests/DataSourceTests.cs @@ -260,4 +260,24 @@ public async Task As_DbDataSource([Values] bool async) ? await command.ExecuteScalarAsync() : command.ExecuteScalar(), Is.EqualTo(1)); } + + [Test, IssueLink("https://github.com/npgsql/npgsql/issues/4840")] + public async Task Multiplexing_connectionless_command_open_connection() + { + var csb = new NpgsqlConnectionStringBuilder(ConnectionString) + { + Multiplexing = true + }; + await using var dataSource = NpgsqlDataSource.Create(csb.ConnectionString); + + await using var conn = await dataSource.OpenConnectionAsync(); + await using var _ = await conn.BeginTransactionAsync(); + + await using var command = dataSource.CreateCommand(); + command.CommandText = "SELECT 1"; + + await using var reader = await command.ExecuteReaderAsync(); + Assert.True(reader.Read()); + Assert.That(reader.GetInt32(0), Is.EqualTo(1)); + } }