Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
018196a
Emit pool metrics and fix Count semantics in ChannelDbConnectionPool
mdaigle Aug 12, 2026
6d7710d
Add pool tracing parity for ChannelDbConnectionPool
mdaigle Aug 12, 2026
6f0261c
Give connection pools an injectable metrics sink
mdaigle Aug 12, 2026
787823b
Define the pool metrics seam as an interface
mdaigle Aug 12, 2026
2a0ed09
Remove trace-message parity tests
mdaigle Aug 13, 2026
efd55a0
Fix trace-message class/method references in pool V2
mdaigle Aug 13, 2026
c63d491
Adopt new trace message format in pool V2
mdaigle Aug 13, 2026
56731d2
Simplify pool V2 trace call sites
mdaigle Aug 13, 2026
2e0dcf9
Add missing coverage traces for warmup and connection retrieval
mdaigle Aug 13, 2026
a005f3c
Feed connection metrics through DbConnectionInternal's constructor
mdaigle Aug 13, 2026
21644e4
Avoid duplicating base constructor defaults; drop dead pool helper
mdaigle Aug 13, 2026
a970bdd
Clean up test file structure.
mdaigle Aug 13, 2026
14dbff1
Balance soft and hard disconnects when replacing a connection
mdaigle Aug 14, 2026
563543e
Balance the pooled-connection gauge when replacing a connection
mdaigle Aug 14, 2026
389d934
Address review feedback on metrics routing, sizing, and shared traces
mdaigle Aug 14, 2026
d36665e
Balance connection counters when activation fails
mdaigle Aug 14, 2026
37307b8
Add coverage for the counters emitted when idle connections are pruned
mdaigle Aug 14, 2026
b8ec7a3
Keep the new trace format in PoolPruner
mdaigle Aug 14, 2026
c9dfe1c
Add unit coverage for the stasis connection counter
mdaigle Aug 14, 2026
53dd42c
Merge branch 'main' of https://github.com/dotnet/SqlClient into dev/a…
mdaigle Aug 14, 2026
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Data.Common;
using Microsoft.Data.SqlClient;
using Microsoft.Data.SqlClient.ConnectionPool;
using Microsoft.Data.SqlClient.Diagnostics;
using Microsoft.Data.SqlClient.Internal;

#if NETFRAMEWORK
Expand Down Expand Up @@ -75,8 +76,20 @@ protected DbConnectionInternal() : this(ConnectionState.Open, true, false)
{
}

// Constructor for internal connections that report to a specific metrics sink, reusing
// the same defaults as the parameterless constructor above.
protected DbConnectionInternal(ISqlClientMetrics metrics) : this(ConnectionState.Open, true, false, metrics)
{
}

// Constructor for internal connections
internal DbConnectionInternal(ConnectionState state, bool hidePassword, bool allowSetConnectionString)
: this(state, hidePassword, allowSetConnectionString, metrics: null)
{
}

// Constructor for internal connections
internal DbConnectionInternal(ConnectionState state, bool hidePassword, bool allowSetConnectionString, ISqlClientMetrics metrics)
{
AllowSetConnectionString = allowSetConnectionString;
ShouldHidePassword = hidePassword;
Expand All @@ -87,6 +100,7 @@ internal DbConnectionInternal(ConnectionState state, bool hidePassword, bool all
// Without this initialization, ReturnedTime would default to DateTime.MinValue, which would cause
// IsLiveConnection to immediately evict every new connection whenever IdleTimeout is configured.
ReturnedTime = CreateTime;
Metrics = metrics ?? SqlClientDiagnostics.Metrics;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only ActivateConnection and DeactivateConnection were moved onto this. SetInStasis, TerminateStasis, the non-pooled HardDisconnectRequest and both Enter/ExitNonPooledConnection calls still go to SqlClientDiagnostics.Metrics, so one object reports to two sinks depending on which counter fires.

The test consequence is the part I care about: AssertCounters claims any counter it doesn't name must be zero, but stasisConnections and nonPooledConnections can never be non-zero on the fake. Those rows pass vacuously and give us false confidence, and stasis is the counter sitting right next to the #3640 double-deactivation we're regression testing here.

Can we route the rest through the instance too? If there's a reason not to, the vacuous rows should come out of AssertCounters with a note saying why.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Routed the remaining sites through the instance sink.

Worth noting this is a no-op in production. The factory hands every connection the process-wide instance, so Metrics already is SqlClientDiagnostics.Metrics there. The value is that a test can now observe those counters.

}

#region Properties
Expand Down Expand Up @@ -179,6 +193,16 @@ internal bool IsInPool
/// </summary>
internal IDbConnectionPool Pool { get; private set; }

/// <summary>
/// The metrics sink this connection reports its activation state to. Supplied by the
/// <see cref="SqlConnectionFactory"/> that created this connection, from its own injected
/// instance, rather than derived from <see cref="Pool"/>: the metrics sink is not
/// inherently tied to a pool, and a connection is not necessarily pooled at all. Defaults
/// to the process-wide instance when no metrics sink is supplied to the constructor (e.g.
/// a test double constructed directly), so it still reports somewhere.
/// </summary>
internal ISqlClientMetrics Metrics { get; }

public abstract string ServerVersion { get; }

public virtual ConnectionCapabilities Capabilities => null;
Expand Down Expand Up @@ -366,9 +390,13 @@ internal void ActivateConnection(Transaction transaction)
// the Activate method publicly.
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionInternal.ActivateConnection|RES|INFO|CPOOL> {0}, Activating", ObjectID);

Activate(transaction);
// Counted before Activate, mirroring DeactivateConnection, which counts before
// Deactivate. If Activate throws, the pool returns the connection and deactivates it,
// so counting afterwards would leave that exit unpaired and drive the
// active-connections gauge negative.
Metrics.EnterActiveConnection();

SqlClientDiagnostics.Metrics.EnterActiveConnection();
Activate(transaction);
}

internal void AddWeakReference(object value, int tag)
Expand Down Expand Up @@ -481,7 +509,7 @@ internal virtual void CloseConnection(DbConnection owningObject, SqlConnectionFa
// and transactions may not get cleaned up...
Deactivate();

SqlClientDiagnostics.Metrics.HardDisconnectRequest();
Metrics.HardDisconnectRequest();

// To prevent an endless recursion, we need to clear the owning object
// before we call dispose so that we can't get here a second time...
Expand All @@ -496,7 +524,7 @@ internal virtual void CloseConnection(DbConnection owningObject, SqlConnectionFa
}
else
{
SqlClientDiagnostics.Metrics.ExitNonPooledConnection();
Metrics.ExitNonPooledConnection();
Dispose();
}
}
Expand All @@ -522,7 +550,7 @@ internal void DeactivateConnection()
// the Deactivate method publicly.
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionInternal.DeactivateConnection|RES|INFO|CPOOL> {0}, Deactivating", ObjectID);

SqlClientDiagnostics.Metrics.ExitActiveConnection();
Metrics.ExitActiveConnection();

if (!IsConnectionDoomed && Pool.UseLoadBalancing)
{
Expand Down Expand Up @@ -582,7 +610,7 @@ internal virtual void DelegatedTransactionEnded()
// once and for all, or the server will have fits about us
// leaving connections open until the client-side GC kicks
// in.
SqlClientDiagnostics.Metrics.ExitNonPooledConnection();
Metrics.ExitNonPooledConnection();

Dispose();
}
Expand Down Expand Up @@ -812,7 +840,7 @@ internal void SetInStasis()
IsTxRootWaitingForTxEnd = true;
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionInternal.SetInStasis|RES|CPOOL> {0}, Non-Pooled Connection has Delegated Transaction, waiting to Dispose.", ObjectID);

SqlClientDiagnostics.Metrics.EnterStasisConnection();
Metrics.EnterStasisConnection();
}

/// <remarks>
Expand Down Expand Up @@ -998,7 +1026,7 @@ private void TerminateStasis(bool returningToPool)
: "Delegated Transaction has ended, connection is closed/leaked. Disposing.";
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionInternal.TerminateStasis|RES|CPOOL> {0}, {1}", ObjectID, message);

SqlClientDiagnostics.Metrics.ExitStasisConnection();
Metrics.ExitStasisConnection();

IsTxRootWaitingForTxEnd = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.Data.Common;
using Microsoft.Data.ProviderBase;
using Microsoft.Data.SqlClient.ConnectionPool;
using Microsoft.Data.SqlClient.Diagnostics;
using Microsoft.Data.SqlClient.Internal;
using Microsoft.Data.SqlClient.Utilities;
using IsolationLevel = System.Data.IsolationLevel;
Expand Down Expand Up @@ -306,7 +307,9 @@ internal SqlConnectionInternal(
string accessToken = null,
IDbConnectionPool pool = null,
Func<SqlAuthenticationParameters, CancellationToken, Task<SqlAuthenticationToken>> accessTokenCallback = null,
SspiContextProvider sspiContextProvider = null)
SspiContextProvider sspiContextProvider = null,
ISqlClientMetrics metrics = null)
: base(metrics)
{
Debug.Assert(connectionOptions is not null, "null connectionOptions");

Expand Down
Loading
Loading