Skip to content

Commit 5461fa8

Browse files
authored
Allow returning non-empty NpgsqlConection.DataSource with closed conn… (#4987)
* Allow returning non-empty NpgsqlConection.DataSource with closed connection if single host Closes #4985
1 parent dbfae1a commit 5461fa8

3 files changed

Lines changed: 16 additions & 10 deletions

File tree

src/Npgsql/NpgsqlConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public override string ConnectionString
449449
/// The name of the database server (host and port). If the connection uses a Unix-domain socket,
450450
/// the path to that socket is returned. The default value is the empty string.
451451
/// </value>
452-
public override string DataSource => Connector?.Settings.DataSourceCached ?? string.Empty;
452+
public override string DataSource => Connector?.Settings.DataSourceCached ?? _dataSource?.Settings.DataSourceCached ?? string.Empty;
453453

454454
/// <summary>
455455
/// Whether to use Windows integrated security to log in.

src/Npgsql/NpgsqlConnectionStringBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public sealed partial class NpgsqlConnectionStringBuilder : DbConnectionStringBu
2727
/// </summary>
2828
string? _dataSourceCached;
2929

30-
internal string DataSourceCached
31-
=> _dataSourceCached ??= _host is null
32-
? string.Empty
30+
internal string? DataSourceCached
31+
=> _dataSourceCached ??= _host is null || _host.Contains(',')
32+
? null
3333
: IsUnixSocket(_host, _port, out var socketPath, replaceForAbstract: false)
3434
? socketPath
3535
: $"tcp://{_host}:{_port}";

test/Npgsql.Tests/ConnectionTests.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,17 +500,23 @@ public async Task Unix_abstract_domain_socket()
500500
}
501501

502502
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/903")]
503-
public async Task DataSource_property()
503+
public void DataSource_property()
504504
{
505505
using var conn = new NpgsqlConnection();
506506
Assert.That(conn.DataSource, Is.EqualTo(string.Empty));
507507

508-
conn.ConnectionString = ConnectionString;
509-
Assert.That(conn.DataSource, Is.EqualTo(string.Empty));
508+
var csb = new NpgsqlConnectionStringBuilder(ConnectionString);
510509

511-
await conn.OpenAsync();
512-
await using var _ = await conn.BeginTransactionAsync();
513-
Assert.That(conn.DataSource, Is.EqualTo($"tcp://{conn.Host}:{conn.Port}"));
510+
conn.ConnectionString = csb.ConnectionString;
511+
Assert.That(conn.DataSource, Is.EqualTo($"tcp://{csb.Host}:{csb.Port}"));
512+
513+
// Multiplexing isn't supported with multiple hosts
514+
if (IsMultiplexing)
515+
return;
516+
517+
csb.Host = "127.0.0.1, 127.0.0.2";
518+
conn.ConnectionString = csb.ConnectionString;
519+
Assert.That(conn.DataSource, Is.EqualTo(string.Empty));
514520
}
515521

516522
#region Server version

0 commit comments

Comments
 (0)