Skip to content

Commit e9ab0a8

Browse files
committed
@vonzshik's suggestion
1 parent 3d1e34c commit e9ab0a8

4 files changed

Lines changed: 32 additions & 13 deletions

File tree

src/Npgsql/NpgsqlConnection.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ public sealed class NpgsqlConnection : DbConnection, ICloneable, IComponent
3535
// Set this when disposed is called.
3636
bool _disposed;
3737

38-
// Set to true when the private NpgsqlDataSource is created by setting the ConnectionString property (legacy code path)
39-
// in which case we want to retry if the DataSource has been disposed by the PoolManager as a reaction
40-
// to Clean() or CleanAll()
41-
bool _retryDisposedDataSource;
42-
4338
/// <summary>
4439
/// The connection string, without the password after open (unless Persist Security Info=true)
4540
/// </summary>
@@ -227,6 +222,7 @@ void SetupDataSource()
227222
var dataSourceBuilder = new NpgsqlDataSourceBuilder(canonical);
228223
dataSourceBuilder.UseLoggerFactory(NpgsqlLoggingConfiguration.GlobalLoggerFactory);
229224
dataSourceBuilder.EnableParameterLogging(NpgsqlLoggingConfiguration.GlobalIsParameterLoggingEnabled);
225+
dataSourceBuilder.CreateLegacyDataSource();
230226
var newDataSource = dataSourceBuilder.Build();
231227

232228
_dataSource = PoolManager.GetOrAddPool(canonical, newDataSource);
@@ -369,11 +365,11 @@ async Task PerformMultiplexingStartupCheck(bool async, CancellationToken cancell
369365
}
370366
async Task<NpgsqlConnector> GetConnector(NpgsqlTimeout timeout, bool async, CancellationToken cancellationToken)
371367
{
368+
Debug.Assert(_dataSource != null);
372369
while (true)
373370
{
374371
try
375372
{
376-
Debug.Assert(_dataSource != null);
377373
return await _dataSource.Get(this, timeout, async, cancellationToken);
378374
}
379375
// When using PoolManager (legacy code path setting NpgsqlConnection.ConnectionString), clearing a pool
@@ -384,7 +380,7 @@ async Task<NpgsqlConnector> GetConnector(NpgsqlTimeout timeout, bool async, Canc
384380
// _retryDisposedDataSource is false) we bubble up an eventual ObjectDisposedException as usual.
385381
catch (ObjectDisposedException)
386382
{
387-
if (!_retryDisposedDataSource)
383+
if (!_dataSource.IsLegacyDataSource)
388384
throw;
389385
SetupDataSource();
390386
}
@@ -411,7 +407,6 @@ public override string ConnectionString
411407
CheckClosed();
412408

413409
_userFacingConnectionString = _connectionString = value ?? string.Empty;
414-
_retryDisposedDataSource = true;
415410
SetupDataSource();
416411
}
417412
}
@@ -1937,11 +1932,12 @@ public Task ReloadTypesAsync()
19371932

19381933
async Task BootstrapDataSource(NpgsqlConnector connector, bool async)
19391934
{
1935+
Debug.Assert(_dataSource != null);
19401936
while (true)
19411937
{
19421938
try
19431939
{
1944-
await _dataSource!.Bootstrap(
1940+
await _dataSource.Bootstrap(
19451941
connector,
19461942
NpgsqlTimeout.Infinite,
19471943
forceReload: true,
@@ -1957,7 +1953,7 @@ async Task BootstrapDataSource(NpgsqlConnector connector, bool async)
19571953
// _retryDisposedDataSource is false) we bubble up an eventual ObjectDisposedException as usual.
19581954
catch (ObjectDisposedException)
19591955
{
1960-
if (!_retryDisposedDataSource)
1956+
if (!_dataSource.IsLegacyDataSource)
19611957
throw;
19621958
SetupDataSource();
19631959
}

src/Npgsql/NpgsqlDataSource.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public abstract class NpgsqlDataSource : DbDataSource
3434
internal NpgsqlDataSourceConfiguration Configuration { get; }
3535
internal NpgsqlLoggingConfiguration LoggingConfiguration { get; }
3636

37+
/// <summary>
38+
/// <see langword="true"/> if the connection was created by setting the <see cref="NpgsqlConnection.ConnectionString"/>
39+
/// property in a <see cref="NpgsqlConnection"/>; otherwise false.
40+
/// </summary>
41+
internal bool IsLegacyDataSource { get; }
42+
3743
readonly List<TypeHandlerResolverFactory> _resolverFactories;
3844
readonly Dictionary<string, IUserTypeMapping> _userTypeMappings;
3945
readonly INpgsqlNameTranslator _defaultNameTranslator;
@@ -100,7 +106,8 @@ internal NpgsqlDataSource(
100106
_userTypeMappings,
101107
_defaultNameTranslator,
102108
ConnectionInitializer,
103-
ConnectionInitializerAsync)
109+
ConnectionInitializerAsync,
110+
IsLegacyDataSource)
104111
= dataSourceConfig;
105112
_connectionLogger = LoggingConfiguration.ConnectionLogger;
106113

src/Npgsql/NpgsqlDataSourceBuilder.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class NpgsqlDataSourceBuilder : INpgsqlTypeMapper
2222
{
2323
ILoggerFactory? _loggerFactory;
2424
bool _sensitiveDataLoggingEnabled;
25+
bool _isLegacyDataSource;
2526

2627
RemoteCertificateValidationCallback? _userCertificateValidationCallback;
2728
Action<X509CertificateCollection>? _clientCertificatesCallback;
@@ -333,6 +334,19 @@ public NpgsqlDataSourceBuilder UsePhysicalConnectionInitializer(
333334
return this;
334335
}
335336

337+
/// <summary>
338+
/// Use this method to flag a NpgsqlDataSource as legacy data source which means that the
339+
/// connection was created by setting the <see cref="NpgsqlConnection.ConnectionString"/>
340+
/// property in a <see cref="NpgsqlConnection"/>
341+
/// </summary>
342+
/// <param name="isLegacyDataSource">If <see langword="true" />, then the NpgsqlDataSource is flagged as legacy data source.</param>
343+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
344+
internal NpgsqlDataSourceBuilder CreateLegacyDataSource(bool isLegacyDataSource = true)
345+
{
346+
_isLegacyDataSource = isLegacyDataSource;
347+
return this;
348+
}
349+
336350
/// <summary>
337351
/// Builds and returns an <see cref="NpgsqlDataSource" /> which is ready for use.
338352
/// </summary>
@@ -389,7 +403,8 @@ _loggerFactory is null
389403
_userTypeMappings,
390404
DefaultNameTranslator,
391405
_syncConnectionInitializer,
392-
_asyncConnectionInitializer);
406+
_asyncConnectionInitializer,
407+
_isLegacyDataSource);
393408
}
394409

395410
void ValidateMultiHost()

src/Npgsql/NpgsqlDataSourceConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ sealed record NpgsqlDataSourceConfiguration(
2020
Dictionary<string, IUserTypeMapping> UserTypeMappings,
2121
INpgsqlNameTranslator DefaultNameTranslator,
2222
Action<NpgsqlConnection>? ConnectionInitializer,
23-
Func<NpgsqlConnection, Task>? ConnectionInitializerAsync);
23+
Func<NpgsqlConnection, Task>? ConnectionInitializerAsync,
24+
bool IsLegacyDataSource);

0 commit comments

Comments
 (0)