Skip to content

Commit

Permalink
Convert try-finally log scopes to using blocks (#1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraith2 committed Aug 4, 2021
1 parent df9990c commit 83b8421
Show file tree
Hide file tree
Showing 23 changed files with 1,735 additions and 1,855 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ abstract public DbProviderFactory ProviderFactory

public void ClearAllPools()
{
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("<prov.DbConnectionFactory.ClearAllPools|API");
try
using (TryEventScope.Create("<prov.DbConnectionFactory.ClearAllPools|API"))
{
Dictionary<DbConnectionPoolKey, DbConnectionPoolGroup> connectionPoolGroups = _connectionPoolGroups;
foreach (KeyValuePair<DbConnectionPoolKey, DbConnectionPoolGroup> entry in connectionPoolGroups)
Expand All @@ -62,48 +61,33 @@ public void ClearAllPools()
}
}
}
finally
{
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
}
}

public void ClearPool(DbConnection connection)
{
ADP.CheckArgumentNull(connection, nameof(connection));
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("<prov.DbConnectionFactory.ClearPool|API> {0}", GetObjectId(connection));
try
using (TryEventScope.Create("<prov.DbConnectionFactory.ClearPool|API> {0}", GetObjectId(connection)))
{
DbConnectionPoolGroup poolGroup = GetConnectionPoolGroup(connection);
if (null != poolGroup)
{
poolGroup.Clear();
}
}
finally
{
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
}
}

public void ClearPool(DbConnectionPoolKey key)
{
Debug.Assert(key != null, "key cannot be null");
ADP.CheckArgumentNull(key.ConnectionString, nameof(key) + "." + nameof(key.ConnectionString));
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("<prov.DbConnectionFactory.ClearPool|API> connectionString");
try
using (TryEventScope.Create("<prov.DbConnectionFactory.ClearPool|API> connectionString"))
{
DbConnectionPoolGroup poolGroup;
Dictionary<DbConnectionPoolKey, DbConnectionPoolGroup> connectionPoolGroups = _connectionPoolGroups;
if (connectionPoolGroups.TryGetValue(key, out poolGroup))
if (connectionPoolGroups.TryGetValue(key, out DbConnectionPoolGroup poolGroup))
{
poolGroup.Clear();
}
}
finally
{
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
}
}

internal virtual DbConnectionPoolProviderInfo CreateConnectionPoolProviderInfo(DbConnectionOptions connectionOptions)
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ internal void Dispose()
internal int ExecuteNonQuery()
{
ValidateCommandBehavior(nameof(ExecuteNonQuery), CommandBehavior.Default);
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("SqlCommandSet.ExecuteNonQuery | API | Object Id {0}, Commands executed in Batch RPC mode", ObjectID);

try
using (TryEventScope.Create("SqlCommandSet.ExecuteNonQuery | API | Object Id {0}, Commands executed in Batch RPC mode", ObjectID))
{
BatchCommand.BatchRPCMode = true;
BatchCommand.ClearBatchCommand();
Expand All @@ -293,10 +291,6 @@ internal int ExecuteNonQuery()

return BatchCommand.ExecuteBatchRPCCommand();
}
finally
{
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
}
}

internal SqlParameter GetParameter(int commandIndex, int parameterIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1034,8 +1034,7 @@ public SqlTransaction BeginTransaction(string transactionName)
[SuppressMessage("Microsoft.Reliability", "CA2004:RemoveCallsToGCKeepAlive")]
override protected DbTransaction BeginDbTransaction(System.Data.IsolationLevel isolationLevel)
{
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("SqlConnection.BeginDbTransaction | API | Object Id {0}, Isolation Level {1}", ObjectID, (int)isolationLevel);
try
using (TryEventScope.Create("SqlConnection.BeginDbTransaction | API | Object Id {0}, Isolation Level {1}", ObjectID, (int)isolationLevel))
{
DbTransaction transaction = BeginTransaction(isolationLevel);

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

return transaction;
}
finally
{
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
}
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/BeginTransactionIsoTransactionName/*' />
public SqlTransaction BeginTransaction(System.Data.IsolationLevel iso, string transactionName)
{
WaitForPendingReconnection();
SqlStatistics statistics = null;
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("SqlConnection.BeginTransaction | API | Object Id {0}, Iso {1}, Transaction Name '{2}'", ObjectID, (int)iso, transactionName);
try
using (TryEventScope.Create(SqlClientEventSource.Log.TryScopeEnterEvent("SqlConnection.BeginTransaction | API | Object Id {0}, Iso {1}, Transaction Name '{2}'", ObjectID, (int)iso, transactionName)))
{
statistics = SqlStatistics.StartTimer(Statistics);

SqlTransaction transaction;
bool isFirstAttempt = true;
do
try
{
transaction = GetOpenTdsConnection().BeginSqlTransaction(iso, transactionName, isFirstAttempt); // do not reconnect twice
Debug.Assert(isFirstAttempt || !transaction.InternalTransaction.ConnectionHasBeenRestored, "Restored connection on non-first attempt");
isFirstAttempt = false;
} while (transaction.InternalTransaction.ConnectionHasBeenRestored);
statistics = SqlStatistics.StartTimer(Statistics);

SqlTransaction transaction;
bool isFirstAttempt = true;
do
{
transaction = GetOpenTdsConnection().BeginSqlTransaction(iso, transactionName, isFirstAttempt); // do not reconnect twice
Debug.Assert(isFirstAttempt || !transaction.InternalTransaction.ConnectionHasBeenRestored, "Restored connection on non-first attempt");
isFirstAttempt = false;
} while (transaction.InternalTransaction.ConnectionHasBeenRestored);

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

return transaction;
}
finally
{
SqlStatistics.StopTimer(statistics);
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
// The GetOpenConnection line above doesn't keep a ref on the outer connection (this),
// and it could be collected before the inner connection can hook it to the transaction, resulting in
// a transaction with a null connection property. Use GC.KeepAlive to ensure this doesn't happen.
GC.KeepAlive(this);

return transaction;
}
finally
{
SqlStatistics.StopTimer(statistics);
}
}
}

Expand Down Expand Up @@ -1137,10 +1133,10 @@ private void CloseInnerConnection()
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/Close/*' />
public override void Close()
{
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("SqlConnection.Close | API | Object Id {0}", ObjectID);
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.Close | API | Correlation | Object Id {0}, Activity Id {1}, Client Connection Id {2}", ObjectID, ActivityCorrelator.Current, ClientConnectionId);
try
using (TryEventScope.Create("SqlConnection.Close | API | Object Id {0}", ObjectID))
{
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.Close | API | Correlation | Object Id {0}, Activity Id {1}, Client Connection Id {2}", ObjectID, ActivityCorrelator.Current, ClientConnectionId);

ConnectionState previousState = State;
Guid operationId = default(Guid);
Guid clientConnectionId = default(Guid);
Expand Down Expand Up @@ -1214,10 +1210,6 @@ public override void Close()
}
}
}
finally
{
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
}
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/CreateCommand/*' />
Expand Down Expand Up @@ -1260,10 +1252,10 @@ private bool TryOpenWithRetry(TaskCompletionSource<DbConnectionInternal> retry,
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/OpenWithOverrides/*' />
public void Open(SqlConnectionOverrides overrides)
{
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("SqlConnection.Open | API | Correlation | Object Id {0}, Activity Id {1}", ObjectID, ActivityCorrelator.Current);
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.Open | API | Correlation | Object Id {0}, Activity Id {1}", ObjectID, ActivityCorrelator.Current);
try
using (TryEventScope.Create("SqlConnection.Open | API | Correlation | Object Id {0}, Activity Id {1}", ObjectID, ActivityCorrelator.Current))
{
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.Open | API | Correlation | Object Id {0}, Activity Id {1}", ObjectID, ActivityCorrelator.Current);

Guid operationId = s_diagnosticListener.WriteConnectionOpenBefore(this);

PrepareStatisticsForNewConnection();
Expand Down Expand Up @@ -1298,10 +1290,6 @@ public void Open(SqlConnectionOverrides overrides)
}
}
}
finally
{
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
}
}

internal void RegisterWaitingForReconnect(Task waitingTask)
Expand Down Expand Up @@ -2023,10 +2011,10 @@ internal void OnInfoMessage(SqlInfoMessageEventArgs imevent, out bool notified)
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/ChangePasswordConnectionStringNewPassword/*' />
public static void ChangePassword(string connectionString, string newPassword)
{
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("SqlConnection.ChangePassword | API | Password change requested.");
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.ChangePassword | API | Correlation | ActivityID {0}", ActivityCorrelator.Current);
try
using (TryEventScope.Create("SqlConnection.ChangePassword | API | Password change requested."))
{
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.ChangePassword | API | Correlation | ActivityID {0}", ActivityCorrelator.Current);

if (string.IsNullOrEmpty(connectionString))
{
throw SQL.ChangePasswordArgumentMissing(nameof(newPassword));
Expand Down Expand Up @@ -2054,19 +2042,15 @@ public static void ChangePassword(string connectionString, string newPassword)

ChangePassword(connectionString, connectionOptions, null, newPassword, null);
}
finally
{
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
}
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/ChangePasswordConnectionStringCredentialNewSecurePassword/*' />
public static void ChangePassword(string connectionString, SqlCredential credential, SecureString newSecurePassword)
{
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("SqlConnection.ChangePassword | API | Password change requested.");
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.ChangePassword | API | Correlation | ActivityID {0}", ActivityCorrelator.Current);
try
using (TryEventScope.Create("SqlConnection.ChangePassword | API | Password change requested."))
{
SqlClientEventSource.Log.TryCorrelationTraceEvent("SqlConnection.ChangePassword | API | Correlation | ActivityID {0}", ActivityCorrelator.Current);

if (string.IsNullOrEmpty(connectionString))
{
throw SQL.ChangePasswordArgumentMissing(nameof(connectionString));
Expand Down Expand Up @@ -2115,10 +2099,6 @@ public static void ChangePassword(string connectionString, SqlCredential credent

ChangePassword(connectionString, connectionOptions, credential, null, newSecurePassword);
}
finally
{
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
}
}

private static void ChangePassword(string connectionString, SqlConnectionString connectionOptions, SqlCredential credential, string newPassword, SecureString newSecurePassword)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,14 @@ internal void AddWeakReference(object value, int tag)
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/CreateDbCommand/*' />
override protected DbCommand CreateDbCommand()
{
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("<prov.DbConnectionHelper.CreateDbCommand|API> {0}", ObjectID);
try
using (TryEventScope.Create("<prov.DbConnectionHelper.CreateDbCommand|API> {0}", ObjectID))
{
DbCommand command = null;
DbProviderFactory providerFactory = ConnectionFactory.ProviderFactory;
command = providerFactory.CreateCommand();
command.Connection = this;
return command;
}
finally
{
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
}
}

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

0 comments on commit 83b8421

Please sign in to comment.