Skip to content

Commit a19cec4

Browse files
authored
Cleanup | SqlInternalConnection properties (#3743)
* Skip flaky ordering test. * Clean up IsNonPoolableTransactionRoot * Make IsEnlistedInTransaction an auto-property. * Make IsGlobalTransaction an auto-property * Make IsGlobalTransactionEnabledForServer an auto-property. * Make PromotedDtcToken an auto-property. * Make IsAzureSqlConnection an auto-property. * Make ConnectionOptions an auto-property. * Formatting and property doc comments. * Move TransactionRequest to its own file. * Formatting and reordering. * Address copilot comments. * Add doc comments for TransactionRequest.
1 parent 2216abd commit a19cec4

File tree

11 files changed

+163
-189
lines changed

11 files changed

+163
-189
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,9 @@
807807
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsValueSetter.cs">
808808
<Link>Microsoft\Data\SqlClient\TdsValueSetter.cs</Link>
809809
</Compile>
810+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TransactionRequest.cs">
811+
<Link>Microsoft\Data\SqlClient\TransactionRequest.cs</Link>
812+
</Compile>
810813
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProvider.cs">
811814
<Link>Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProvider.cs</Link>
812815
</Compile>

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -670,14 +670,6 @@ internal override bool IsLockedForBulkCopy
670670
}
671671
}
672672

673-
internal protected override bool IsNonPoolableTransactionRoot
674-
{
675-
get
676-
{
677-
return IsTransactionRoot && (!Is2008OrNewer || Pool == null);
678-
}
679-
}
680-
681673
internal override bool Is2008OrNewer
682674
{
683675
get
@@ -949,7 +941,9 @@ protected override void InternalDeactivate()
949941
// cause our transaction to be rolled back and the connection
950942
// to be reset. We'll get called again once the delegated
951943
// transaction is completed and we can do it all then.
952-
if (!IsNonPoolableTransactionRoot)
944+
// TODO: I think this logic cares about pooling because the pool
945+
// will handle deactivation of pool-associated transaction roots?
946+
if (!(IsTransactionRoot && Pool == null))
953947
{
954948
Debug.Assert(_parser != null || IsConnectionDoomed, "Deactivating a disposed connection?");
955949
if (_parser != null)
@@ -1488,7 +1482,7 @@ private void OpenLoginEnlist(TimeoutTimer timeout,
14881482
timeout);
14891483
}
14901484

1491-
if (!IsAzureSQLConnection)
1485+
if (!IsAzureSqlConnection)
14921486
{
14931487
// If not a connection to Azure SQL, Readonly with FailoverPartner is not supported
14941488
if (ConnectionOptions.ApplicationIntent == ApplicationIntent.ReadOnly)
@@ -2246,7 +2240,7 @@ internal void OnEnvChange(SqlEnvChange rec)
22462240
dtcToken = rec._newBinValue;
22472241
rec._newBinValue = null;
22482242
}
2249-
PromotedDTCToken = dtcToken;
2243+
PromotedDtcToken = dtcToken;
22502244
break;
22512245

22522246
case TdsEnums.ENV_TRANSACTIONENDED:
@@ -2900,7 +2894,7 @@ internal void OnFeatureExtAck(int featureId, byte[] data)
29002894
throw SQL.ParsingError(ParsingErrorState.CorruptedTdsStream);
29012895
}
29022896

2903-
IsAzureSQLConnection = true;
2897+
IsAzureSqlConnection = true;
29042898

29052899
// Bit 0 for RO/FP support
29062900
if ((data[0] & 1) == 1 && SqlClientEventSource.Log.IsTraceEnabled())

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,9 @@
963963
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsValueSetter.cs">
964964
<Link>Microsoft\Data\SqlClient\TdsValueSetter.cs</Link>
965965
</Compile>
966+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TransactionRequest.cs">
967+
<Link>Microsoft\Data\SqlClient\TransactionRequest.cs</Link>
968+
</Compile>
966969
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Utilities\BufferWriterExtensions.netfx.cs">
967970
<Link>Microsoft\Data\SqlClient\Utilities\BufferWriterExtensions.netfx.cs</Link>
968971
</Compile>

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -680,14 +680,6 @@ internal override bool IsLockedForBulkCopy
680680
}
681681
}
682682

683-
protected internal override bool IsNonPoolableTransactionRoot
684-
{
685-
get
686-
{
687-
return IsTransactionRoot && (!Is2008OrNewer || Pool == null);
688-
}
689-
}
690-
691683
internal override bool Is2008OrNewer
692684
{
693685
get
@@ -957,7 +949,9 @@ protected override void InternalDeactivate()
957949
// cause our transaction to be rolled back and the connection
958950
// to be reset. We'll get called again once the delegated
959951
// transaction is completed and we can do it all then.
960-
if (!IsNonPoolableTransactionRoot)
952+
// TODO: I think this logic cares about pooling because the pool
953+
// will handle deactivation of pool-associated transaction roots?
954+
if (!(IsTransactionRoot && Pool == null))
961955
{
962956
Debug.Assert(_parser != null || IsConnectionDoomed, "Deactivating a disposed connection?");
963957
if (_parser != null)
@@ -1497,7 +1491,7 @@ private void OpenLoginEnlist(TimeoutTimer timeout,
14971491
timeout);
14981492
}
14991493

1500-
if (!IsAzureSQLConnection)
1494+
if (!IsAzureSqlConnection)
15011495
{
15021496
// If not a connection to Azure SQL, Readonly with FailoverPartner is not supported
15031497
if (ConnectionOptions.ApplicationIntent == ApplicationIntent.ReadOnly)
@@ -2303,7 +2297,7 @@ internal void OnEnvChange(SqlEnvChange rec)
23032297
dtcToken = rec._newBinValue;
23042298
rec._newBinValue = null;
23052299
}
2306-
PromotedDTCToken = dtcToken;
2300+
PromotedDtcToken = dtcToken;
23072301
break;
23082302

23092303
case TdsEnums.ENV_TRANSACTIONENDED:
@@ -2946,7 +2940,7 @@ internal void OnFeatureExtAck(int featureId, byte[] data)
29462940
throw SQL.ParsingError(ParsingErrorState.CorruptedTdsStream);
29472941
}
29482942

2949-
IsAzureSQLConnection = true;
2943+
IsAzureSqlConnection = true;
29502944

29512945
// Bit 0 for RO/FP support
29522946
if ((data[0] & 1) == 1 && SqlClientEventSource.Log.IsTraceEnabled())

src/Microsoft.Data.SqlClient/src/Microsoft/Data/ProviderBase/DbConnectionInternal.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -304,19 +304,6 @@ protected bool EnlistedTransactionDisposed
304304
/// </summary>
305305
protected internal bool IsConnectionDoomed { get; private set; }
306306

307-
/// <summary>
308-
/// Is this a connection that must be put in stasis (or is already in stasis) pending the
309-
/// end of its transaction?
310-
/// </summary>
311-
/// <remarks>
312-
/// If you want to have delegated transactions that are non-poolable, you had better
313-
/// override this...
314-
/// </remarks>
315-
protected internal virtual bool IsNonPoolableTransactionRoot
316-
{
317-
get => false;
318-
}
319-
320307
/// <remarks>
321308
/// We use a weak reference to the owning object so we can identify when it has been
322309
/// garbage collected without throwing exceptions.

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/WaitHandleDbConnectionPool.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,8 @@ private void DeactivateObject(DbConnectionInternal obj)
912912
}
913913
else
914914
{
915-
if (obj.IsNonPoolableTransactionRoot)
915+
// TODO: how did we get here if the pool is null?
916+
if (obj.IsTransactionRoot && obj.Pool == null)
916917
{
917918
obj.SetInStasis();
918919
rootTxn = true;

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDelegatedTransaction.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void Initialize()
9292

9393
_internalTransaction = new SqlInternalTransaction(connection, TransactionType.Delegated, null);
9494

95-
connection.ExecuteTransaction(SqlInternalConnection.TransactionRequest.Begin, null, _isolationLevel, _internalTransaction, true);
95+
connection.ExecuteTransaction(TransactionRequest.Begin, null, _isolationLevel, _internalTransaction, true);
9696

9797
// Handle case where ExecuteTran didn't produce a new transaction, but also didn't throw.
9898
if (connection.CurrentTransaction == null)
@@ -135,8 +135,8 @@ public byte[] Promote()
135135
// Now that we've acquired the lock, make sure we still have valid state for this operation.
136136
ValidateActiveOnConnection(connection);
137137

138-
connection.ExecuteTransaction(SqlInternalConnection.TransactionRequest.Promote, null, System.Data.IsolationLevel.Unspecified, _internalTransaction, true);
139-
returnValue = connection.PromotedDTCToken;
138+
connection.ExecuteTransaction(TransactionRequest.Promote, null, System.Data.IsolationLevel.Unspecified, _internalTransaction, true);
139+
returnValue = connection.PromotedDtcToken;
140140

141141
// For Global Transactions, we need to set the Transaction Id since we use a Non-MSDTC Promoter type.
142142
if (connection.IsGlobalTransaction)
@@ -231,7 +231,7 @@ public void Rollback(SinglePhaseEnlistment enlistment)
231231
// If we haven't already rolled back (or aborted) then tell the SQL Server to roll back
232232
if (!_internalTransaction.IsAborted)
233233
{
234-
connection.ExecuteTransaction(SqlInternalConnection.TransactionRequest.Rollback, null, System.Data.IsolationLevel.Unspecified, _internalTransaction, true);
234+
connection.ExecuteTransaction(TransactionRequest.Rollback, null, System.Data.IsolationLevel.Unspecified, _internalTransaction, true);
235235
}
236236
}
237237
catch (SqlException e)
@@ -313,7 +313,7 @@ public void SinglePhaseCommit(SinglePhaseEnlistment enlistment)
313313
_active = false; // set to inactive first, doesn't matter how the rest completes, this transaction is done.
314314
_connection = null; // Set prior to ExecuteTransaction call in case this initiates a TransactionEnd event
315315

316-
connection.ExecuteTransaction(SqlInternalConnection.TransactionRequest.Commit, null, System.Data.IsolationLevel.Unspecified, _internalTransaction, true);
316+
connection.ExecuteTransaction(TransactionRequest.Commit, null, System.Data.IsolationLevel.Unspecified, _internalTransaction, true);
317317
commitException = null;
318318
}
319319
catch (SqlException e)
@@ -446,10 +446,10 @@ private void ValidateActiveOnConnection(SqlInternalConnection connection)
446446
private Guid GetGlobalTxnIdentifierFromToken()
447447
{
448448
#if NET
449-
return new Guid(new ReadOnlySpan<byte>(_connection.PromotedDTCToken, _globalTransactionsTokenVersionSizeInBytes, 16));
449+
return new Guid(new ReadOnlySpan<byte>(_connection.PromotedDtcToken, _globalTransactionsTokenVersionSizeInBytes, 16));
450450
#else
451451
byte[] txnGuid = new byte[16];
452-
Buffer.BlockCopy(_connection.PromotedDTCToken, _globalTransactionsTokenVersionSizeInBytes /* Skip the version */, txnGuid, 0, txnGuid.Length);
452+
Buffer.BlockCopy(_connection.PromotedDtcToken, _globalTransactionsTokenVersionSizeInBytes /* Skip the version */, txnGuid, 0, txnGuid.Length);
453453
return new Guid(txnGuid);
454454
#endif
455455
}

0 commit comments

Comments
 (0)