Skip to content

Commit 83b8421

Browse files
authored
Convert try-finally log scopes to using blocks (#1188)
1 parent df9990c commit 83b8421

File tree

23 files changed

+1735
-1855
lines changed

23 files changed

+1735
-1855
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Common/src/Microsoft/Data/ProviderBase/DbConnectionFactory.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ abstract public DbProviderFactory ProviderFactory
4949

5050
public void ClearAllPools()
5151
{
52-
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("<prov.DbConnectionFactory.ClearAllPools|API");
53-
try
52+
using (TryEventScope.Create("<prov.DbConnectionFactory.ClearAllPools|API"))
5453
{
5554
Dictionary<DbConnectionPoolKey, DbConnectionPoolGroup> connectionPoolGroups = _connectionPoolGroups;
5655
foreach (KeyValuePair<DbConnectionPoolKey, DbConnectionPoolGroup> entry in connectionPoolGroups)
@@ -62,48 +61,33 @@ public void ClearAllPools()
6261
}
6362
}
6463
}
65-
finally
66-
{
67-
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
68-
}
6964
}
7065

7166
public void ClearPool(DbConnection connection)
7267
{
7368
ADP.CheckArgumentNull(connection, nameof(connection));
74-
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("<prov.DbConnectionFactory.ClearPool|API> {0}", GetObjectId(connection));
75-
try
69+
using (TryEventScope.Create("<prov.DbConnectionFactory.ClearPool|API> {0}", GetObjectId(connection)))
7670
{
7771
DbConnectionPoolGroup poolGroup = GetConnectionPoolGroup(connection);
7872
if (null != poolGroup)
7973
{
8074
poolGroup.Clear();
8175
}
8276
}
83-
finally
84-
{
85-
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
86-
}
8777
}
8878

8979
public void ClearPool(DbConnectionPoolKey key)
9080
{
9181
Debug.Assert(key != null, "key cannot be null");
9282
ADP.CheckArgumentNull(key.ConnectionString, nameof(key) + "." + nameof(key.ConnectionString));
93-
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("<prov.DbConnectionFactory.ClearPool|API> connectionString");
94-
try
83+
using (TryEventScope.Create("<prov.DbConnectionFactory.ClearPool|API> connectionString"))
9584
{
96-
DbConnectionPoolGroup poolGroup;
9785
Dictionary<DbConnectionPoolKey, DbConnectionPoolGroup> connectionPoolGroups = _connectionPoolGroups;
98-
if (connectionPoolGroups.TryGetValue(key, out poolGroup))
86+
if (connectionPoolGroups.TryGetValue(key, out DbConnectionPoolGroup poolGroup))
9987
{
10088
poolGroup.Clear();
10189
}
10290
}
103-
finally
104-
{
105-
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
106-
}
10791
}
10892

10993
internal virtual DbConnectionPoolProviderInfo CreateConnectionPoolProviderInfo(DbConnectionOptions connectionOptions)

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs

Lines changed: 237 additions & 234 deletions
Large diffs are not rendered by default.

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommandSet.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,7 @@ internal void Dispose()
278278
internal int ExecuteNonQuery()
279279
{
280280
ValidateCommandBehavior(nameof(ExecuteNonQuery), CommandBehavior.Default);
281-
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("SqlCommandSet.ExecuteNonQuery | API | Object Id {0}, Commands executed in Batch RPC mode", ObjectID);
282-
283-
try
281+
using (TryEventScope.Create("SqlCommandSet.ExecuteNonQuery | API | Object Id {0}, Commands executed in Batch RPC mode", ObjectID))
284282
{
285283
BatchCommand.BatchRPCMode = true;
286284
BatchCommand.ClearBatchCommand();
@@ -293,10 +291,6 @@ internal int ExecuteNonQuery()
293291

294292
return BatchCommand.ExecuteBatchRPCCommand();
295293
}
296-
finally
297-
{
298-
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
299-
}
300294
}
301295

302296
internal SqlParameter GetParameter(int commandIndex, int parameterIndex)

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,8 +1034,7 @@ public SqlTransaction BeginTransaction(string transactionName)
10341034
[SuppressMessage("Microsoft.Reliability", "CA2004:RemoveCallsToGCKeepAlive")]
10351035
override protected DbTransaction BeginDbTransaction(System.Data.IsolationLevel isolationLevel)
10361036
{
1037-
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("SqlConnection.BeginDbTransaction | API | Object Id {0}, Isolation Level {1}", ObjectID, (int)isolationLevel);
1038-
try
1037+
using (TryEventScope.Create("SqlConnection.BeginDbTransaction | API | Object Id {0}, Isolation Level {1}", ObjectID, (int)isolationLevel))
10391038
{
10401039
DbTransaction transaction = BeginTransaction(isolationLevel);
10411040

@@ -1047,43 +1046,40 @@ override protected DbTransaction BeginDbTransaction(System.Data.IsolationLevel i
10471046

10481047
return transaction;
10491048
}
1050-
finally
1051-
{
1052-
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
1053-
}
10541049
}
10551050

10561051
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/BeginTransactionIsoTransactionName/*' />
10571052
public SqlTransaction BeginTransaction(System.Data.IsolationLevel iso, string transactionName)
10581053
{
10591054
WaitForPendingReconnection();
10601055
SqlStatistics statistics = null;
1061-
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("SqlConnection.BeginTransaction | API | Object Id {0}, Iso {1}, Transaction Name '{2}'", ObjectID, (int)iso, transactionName);
1062-
try
1056+
using (TryEventScope.Create(SqlClientEventSource.Log.TryScopeEnterEvent("SqlConnection.BeginTransaction | API | Object Id {0}, Iso {1}, Transaction Name '{2}'", ObjectID, (int)iso, transactionName)))
10631057
{
1064-
statistics = SqlStatistics.StartTimer(Statistics);
1065-
1066-
SqlTransaction transaction;
1067-
bool isFirstAttempt = true;
1068-
do
1058+
try
10691059
{
1070-
transaction = GetOpenTdsConnection().BeginSqlTransaction(iso, transactionName, isFirstAttempt); // do not reconnect twice
1071-
Debug.Assert(isFirstAttempt || !transaction.InternalTransaction.ConnectionHasBeenRestored, "Restored connection on non-first attempt");
1072-
isFirstAttempt = false;
1073-
} while (transaction.InternalTransaction.ConnectionHasBeenRestored);
1060+
statistics = SqlStatistics.StartTimer(Statistics);
10741061

1062+
SqlTransaction transaction;
1063+
bool isFirstAttempt = true;
1064+
do
1065+
{
1066+
transaction = GetOpenTdsConnection().BeginSqlTransaction(iso, transactionName, isFirstAttempt); // do not reconnect twice
1067+
Debug.Assert(isFirstAttempt || !transaction.InternalTransaction.ConnectionHasBeenRestored, "Restored connection on non-first attempt");
1068+
isFirstAttempt = false;
1069+
} while (transaction.InternalTransaction.ConnectionHasBeenRestored);
10751070

1076-
// The GetOpenConnection line above doesn't keep a ref on the outer connection (this),
1077-
// and it could be collected before the inner connection can hook it to the transaction, resulting in
1078-
// a transaction with a null connection property. Use GC.KeepAlive to ensure this doesn't happen.
1079-
GC.KeepAlive(this);
10801071

1081-
return transaction;
1082-
}
1083-
finally
1084-
{
1085-
SqlStatistics.StopTimer(statistics);
1086-
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
1072+
// The GetOpenConnection line above doesn't keep a ref on the outer connection (this),
1073+
// and it could be collected before the inner connection can hook it to the transaction, resulting in
1074+
// a transaction with a null connection property. Use GC.KeepAlive to ensure this doesn't happen.
1075+
GC.KeepAlive(this);
1076+
1077+
return transaction;
1078+
}
1079+
finally
1080+
{
1081+
SqlStatistics.StopTimer(statistics);
1082+
}
10871083
}
10881084
}
10891085

@@ -1137,10 +1133,10 @@ private void CloseInnerConnection()
11371133
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/Close/*' />
11381134
public override void Close()
11391135
{
1140-
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("SqlConnection.Close | API | Object Id {0}", ObjectID);
1141-
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.Close | API | Correlation | Object Id {0}, Activity Id {1}, Client Connection Id {2}", ObjectID, ActivityCorrelator.Current, ClientConnectionId);
1142-
try
1136+
using (TryEventScope.Create("SqlConnection.Close | API | Object Id {0}", ObjectID))
11431137
{
1138+
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.Close | API | Correlation | Object Id {0}, Activity Id {1}, Client Connection Id {2}", ObjectID, ActivityCorrelator.Current, ClientConnectionId);
1139+
11441140
ConnectionState previousState = State;
11451141
Guid operationId = default(Guid);
11461142
Guid clientConnectionId = default(Guid);
@@ -1214,10 +1210,6 @@ public override void Close()
12141210
}
12151211
}
12161212
}
1217-
finally
1218-
{
1219-
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
1220-
}
12211213
}
12221214

12231215
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/CreateCommand/*' />
@@ -1260,10 +1252,10 @@ private bool TryOpenWithRetry(TaskCompletionSource<DbConnectionInternal> retry,
12601252
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/OpenWithOverrides/*' />
12611253
public void Open(SqlConnectionOverrides overrides)
12621254
{
1263-
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("SqlConnection.Open | API | Correlation | Object Id {0}, Activity Id {1}", ObjectID, ActivityCorrelator.Current);
1264-
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.Open | API | Correlation | Object Id {0}, Activity Id {1}", ObjectID, ActivityCorrelator.Current);
1265-
try
1255+
using (TryEventScope.Create("SqlConnection.Open | API | Correlation | Object Id {0}, Activity Id {1}", ObjectID, ActivityCorrelator.Current))
12661256
{
1257+
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.Open | API | Correlation | Object Id {0}, Activity Id {1}", ObjectID, ActivityCorrelator.Current);
1258+
12671259
Guid operationId = s_diagnosticListener.WriteConnectionOpenBefore(this);
12681260

12691261
PrepareStatisticsForNewConnection();
@@ -1298,10 +1290,6 @@ public void Open(SqlConnectionOverrides overrides)
12981290
}
12991291
}
13001292
}
1301-
finally
1302-
{
1303-
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
1304-
}
13051293
}
13061294

13071295
internal void RegisterWaitingForReconnect(Task waitingTask)
@@ -2023,10 +2011,10 @@ internal void OnInfoMessage(SqlInfoMessageEventArgs imevent, out bool notified)
20232011
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/ChangePasswordConnectionStringNewPassword/*' />
20242012
public static void ChangePassword(string connectionString, string newPassword)
20252013
{
2026-
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("SqlConnection.ChangePassword | API | Password change requested.");
2027-
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.ChangePassword | API | Correlation | ActivityID {0}", ActivityCorrelator.Current);
2028-
try
2014+
using (TryEventScope.Create("SqlConnection.ChangePassword | API | Password change requested."))
20292015
{
2016+
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.ChangePassword | API | Correlation | ActivityID {0}", ActivityCorrelator.Current);
2017+
20302018
if (string.IsNullOrEmpty(connectionString))
20312019
{
20322020
throw SQL.ChangePasswordArgumentMissing(nameof(newPassword));
@@ -2054,19 +2042,15 @@ public static void ChangePassword(string connectionString, string newPassword)
20542042

20552043
ChangePassword(connectionString, connectionOptions, null, newPassword, null);
20562044
}
2057-
finally
2058-
{
2059-
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
2060-
}
20612045
}
20622046

20632047
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/ChangePasswordConnectionStringCredentialNewSecurePassword/*' />
20642048
public static void ChangePassword(string connectionString, SqlCredential credential, SecureString newSecurePassword)
20652049
{
2066-
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("SqlConnection.ChangePassword | API | Password change requested.");
2067-
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.ChangePassword | API | Correlation | ActivityID {0}", ActivityCorrelator.Current);
2068-
try
2050+
using (TryEventScope.Create("SqlConnection.ChangePassword | API | Password change requested."))
20692051
{
2052+
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.ChangePassword | API | Correlation | ActivityID {0}", ActivityCorrelator.Current);
2053+
20702054
if (string.IsNullOrEmpty(connectionString))
20712055
{
20722056
throw SQL.ChangePasswordArgumentMissing(nameof(connectionString));
@@ -2115,10 +2099,6 @@ public static void ChangePassword(string connectionString, SqlCredential credent
21152099

21162100
ChangePassword(connectionString, connectionOptions, credential, null, newSecurePassword);
21172101
}
2118-
finally
2119-
{
2120-
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
2121-
}
21222102
}
21232103

21242104
private static void ChangePassword(string connectionString, SqlConnectionString connectionOptions, SqlCredential credential, string newPassword, SecureString newSecurePassword)

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnectionHelper.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,14 @@ internal void AddWeakReference(object value, int tag)
153153
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/CreateDbCommand/*' />
154154
override protected DbCommand CreateDbCommand()
155155
{
156-
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("<prov.DbConnectionHelper.CreateDbCommand|API> {0}", ObjectID);
157-
try
156+
using (TryEventScope.Create("<prov.DbConnectionHelper.CreateDbCommand|API> {0}", ObjectID))
158157
{
159158
DbCommand command = null;
160159
DbProviderFactory providerFactory = ConnectionFactory.ProviderFactory;
161160
command = providerFactory.CreateCommand();
162161
command.Connection = this;
163162
return command;
164163
}
165-
finally
166-
{
167-
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
168-
}
169164
}
170165

171166
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/Dispose/*' />

0 commit comments

Comments
 (0)