Skip to content

Commit

Permalink
Perf: Lazy init reconnect lock when needed (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraith2 authored Feb 5, 2020
1 parent c33ddd1 commit ce76eb0
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public sealed partial class SqlConnection : DbConnection, ICloneable
private string _accessToken; // Access Token to be used for token based authententication

// connection resiliency
private object _reconnectLock = new object();
private object _reconnectLock;
internal Task _currentReconnectionTask;
private Task _asyncWaitingForReconnection; // current async task waiting for reconnection in non-MARS connections
private Guid _originalConnectionId = Guid.Empty;
Expand Down Expand Up @@ -1116,6 +1116,12 @@ internal Task ValidateAndReconnect(Action beforeDisconnect, int timeout)
if (cData._unrecoverableStatesCount == 0)
{
bool callDisconnect = false;

if (_reconnectLock is null)
{
Interlocked.CompareExchange(ref _reconnectLock, new object(), null);
}

lock (_reconnectLock)
{
tdsConn.CheckEnlistedTransactionBinding();
Expand Down

0 comments on commit ce76eb0

Please sign in to comment.