|
4 | 4 | using System; |
5 | 5 | using System.Diagnostics; |
6 | 6 | using System.Reflection; |
| 7 | +using System.Threading.Tasks; |
7 | 8 | using System.Transactions; |
8 | 9 | using Xunit; |
9 | 10 |
|
@@ -177,6 +178,143 @@ public void StasisCounters_Functional() |
177 | 178 | Assert.Equal(0, SqlClientEventSourceProps.StasisConnections); |
178 | 179 | } |
179 | 180 |
|
| 181 | + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))] |
| 182 | + public void TransactedConnectionPool_VerifyActiveConnectionCounters() |
| 183 | + { |
| 184 | + // This test verifies that the active connection count metric never goes negative |
| 185 | + // when connections are returned to the pool while enlisted in a transaction. |
| 186 | + // This is a regression test for issue #3640 where an extra DeactivateConnection |
| 187 | + // call was causing the active connection count to go negative. |
| 188 | + |
| 189 | + // Arrange |
| 190 | + var stringBuilder = new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString) |
| 191 | + { |
| 192 | + Pooling = true, |
| 193 | + Enlist = false, |
| 194 | + MinPoolSize = 0, |
| 195 | + MaxPoolSize = 10 |
| 196 | + }; |
| 197 | + |
| 198 | + // Clear pools to start fresh |
| 199 | + ClearConnectionPools(); |
| 200 | + |
| 201 | + long initialActiveSoftConnections = SqlClientEventSourceProps.ActiveSoftConnections; |
| 202 | + long initialActiveHardConnections = SqlClientEventSourceProps.ActiveHardConnections; |
| 203 | + long initialActiveConnections = SqlClientEventSourceProps.ActiveConnections; |
| 204 | + |
| 205 | + // Act and Assert |
| 206 | + // Verify counters at each step in the lifecycle of a transacted connection |
| 207 | + using (var txScope = new TransactionScope()) |
| 208 | + { |
| 209 | + using (var conn = new SqlConnection(stringBuilder.ToString())) |
| 210 | + { |
| 211 | + conn.Open(); |
| 212 | + conn.EnlistTransaction(System.Transactions.Transaction.Current); |
| 213 | + |
| 214 | + if (SupportsActiveConnectionCounters) |
| 215 | + { |
| 216 | + // Connection should be active |
| 217 | + Assert.Equal(initialActiveSoftConnections + 1, SqlClientEventSourceProps.ActiveSoftConnections); |
| 218 | + Assert.Equal(initialActiveHardConnections + 1, SqlClientEventSourceProps.ActiveHardConnections); |
| 219 | + Assert.Equal(initialActiveConnections + 1, SqlClientEventSourceProps.ActiveConnections); |
| 220 | + } |
| 221 | + |
| 222 | + conn.Close(); |
| 223 | + |
| 224 | + // Connection is returned to pool but still in transaction (stasis) |
| 225 | + if (SupportsActiveConnectionCounters) |
| 226 | + { |
| 227 | + // Connection should be deactivated (returned to pool) |
| 228 | + Assert.Equal(initialActiveSoftConnections, SqlClientEventSourceProps.ActiveSoftConnections); |
| 229 | + Assert.Equal(initialActiveHardConnections + 1, SqlClientEventSourceProps.ActiveHardConnections); |
| 230 | + Assert.Equal(initialActiveConnections, SqlClientEventSourceProps.ActiveConnections); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + // Completing the transaction after the connection is closed ensures that the connection |
| 235 | + // is in the transacted pool at the time the transaction ends. This verifies that the |
| 236 | + // transition from the transacted pool back to the main pool properly updates the counters. |
| 237 | + txScope.Complete(); |
| 238 | + } |
| 239 | + |
| 240 | + if (SupportsActiveConnectionCounters) |
| 241 | + { |
| 242 | + Assert.Equal(initialActiveSoftConnections, SqlClientEventSourceProps.ActiveSoftConnections); |
| 243 | + Assert.Equal(initialActiveHardConnections + 1, SqlClientEventSourceProps.ActiveHardConnections); |
| 244 | + Assert.Equal(initialActiveConnections, SqlClientEventSourceProps.ActiveConnections); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + #if NET |
| 249 | + // Note: DbConnection.CloseAsync is not available in .NET Framework |
| 250 | + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))] |
| 251 | + public async Task TransactedConnectionPool_VerifyActiveConnectionCounters_Async() |
| 252 | + { |
| 253 | + // This test verifies that the active connection count metric never goes negative |
| 254 | + // when connections are returned to the pool while enlisted in a transaction. |
| 255 | + // This is a regression test for issue #3640 where an extra DeactivateConnection |
| 256 | + // call was causing the active connection count to go negative. |
| 257 | + |
| 258 | + // Arrange |
| 259 | + var stringBuilder = new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString) |
| 260 | + { |
| 261 | + Pooling = true, |
| 262 | + Enlist = false, |
| 263 | + MinPoolSize = 0, |
| 264 | + MaxPoolSize = 10 |
| 265 | + }; |
| 266 | + |
| 267 | + // Clear pools to start fresh |
| 268 | + ClearConnectionPools(); |
| 269 | + |
| 270 | + long initialActiveSoftConnections = SqlClientEventSourceProps.ActiveSoftConnections; |
| 271 | + long initialActiveHardConnections = SqlClientEventSourceProps.ActiveHardConnections; |
| 272 | + long initialActiveConnections = SqlClientEventSourceProps.ActiveConnections; |
| 273 | + |
| 274 | + // Act and Assert |
| 275 | + // Verify counters at each step in the lifecycle of a transacted connection |
| 276 | + using (var txScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) |
| 277 | + { |
| 278 | + using (var conn = new SqlConnection(stringBuilder.ToString())) |
| 279 | + { |
| 280 | + await conn.OpenAsync(); |
| 281 | + conn.EnlistTransaction(System.Transactions.Transaction.Current); |
| 282 | + |
| 283 | + if (SupportsActiveConnectionCounters) |
| 284 | + { |
| 285 | + // Connection should be active |
| 286 | + Assert.Equal(initialActiveSoftConnections + 1, SqlClientEventSourceProps.ActiveSoftConnections); |
| 287 | + Assert.Equal(initialActiveHardConnections + 1, SqlClientEventSourceProps.ActiveHardConnections); |
| 288 | + Assert.Equal(initialActiveConnections + 1, SqlClientEventSourceProps.ActiveConnections); |
| 289 | + } |
| 290 | + |
| 291 | + await conn.CloseAsync(); |
| 292 | + |
| 293 | + // Connection is returned to pool but still in transaction (stasis) |
| 294 | + if (SupportsActiveConnectionCounters) |
| 295 | + { |
| 296 | + // Connection should be deactivated (returned to pool) |
| 297 | + Assert.Equal(initialActiveSoftConnections, SqlClientEventSourceProps.ActiveSoftConnections); |
| 298 | + Assert.Equal(initialActiveHardConnections + 1, SqlClientEventSourceProps.ActiveHardConnections); |
| 299 | + Assert.Equal(initialActiveConnections, SqlClientEventSourceProps.ActiveConnections); |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + // Completing the transaction after the connection is closed ensures that the connection |
| 304 | + // is in the transacted pool at the time the transaction ends. This verifies that the |
| 305 | + // transition from the transacted pool back to the main pool properly updates the counters. |
| 306 | + txScope.Complete(); |
| 307 | + } |
| 308 | + |
| 309 | + if (SupportsActiveConnectionCounters) |
| 310 | + { |
| 311 | + Assert.Equal(initialActiveSoftConnections, SqlClientEventSourceProps.ActiveSoftConnections); |
| 312 | + Assert.Equal(initialActiveHardConnections + 1, SqlClientEventSourceProps.ActiveHardConnections); |
| 313 | + Assert.Equal(initialActiveConnections, SqlClientEventSourceProps.ActiveConnections); |
| 314 | + } |
| 315 | + } |
| 316 | + #endif |
| 317 | + |
180 | 318 | [ActiveIssue("https://github.com/dotnet/SqlClient/issues/3031")] |
181 | 319 | [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))] |
182 | 320 | public void ReclaimedConnectionsCounter_Functional() |
|
0 commit comments