Skip to content

Commit

Permalink
Require global opt-in for distributed transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Sep 29, 2022
1 parent 3fed4a3 commit 198054d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ public static partial class TransactionManager
[System.Diagnostics.CodeAnalysis.DisallowNullAttribute]
public static System.Transactions.HostCurrentTransactionCallback? HostCurrentCallback { get { throw null; } set { } }
public static System.TimeSpan MaximumTimeout { get { throw null; } set { } }
public static bool ImplicitDistributedTransactions { get; set; }
public static event System.Transactions.TransactionStartedEventHandler? DistributedTransactionStarted { add { } remove { } }
public static void RecoveryComplete(System.Guid resourceManagerIdentifier) { }
public static System.Transactions.Enlistment Reenlist(System.Guid resourceManagerIdentifier, byte[] recoveryInformation, System.Transactions.IEnlistmentNotification enlistmentNotification) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,7 @@
<data name="DistributedNotSupportedOn32Bits" xml:space="preserve">
<value>Distributed transactions are currently unsupported in 32-bit processes.</value>
</data>
</root>
<data name="ImplicitDistributedTransactionsDisabled" xml:space="preserve">
<value>Implicit distributed transactions have not been enabled. If you're intentionally starting a distributed transaction, set TransactionManager.ImplicitDistributedTransactions to true.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ public void ConnectToProxy(
out byte[] whereabouts,
out ResourceManagerShim resourceManagerShim)
{
switch (RuntimeInformation.ProcessArchitecture)
if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
{
case Architecture.X86:
throw new PlatformNotSupportedException(SR.DistributedNotSupportedOn32Bits);
throw new PlatformNotSupportedException(SR.DistributedNotSupportedOn32Bits);
}

if (!TransactionManager.ImplicitDistributedTransactions)
{
throw new NotSupportedException(SR.ImplicitDistributedTransactionsDisabled);
}

ConnectToProxyCore(nodeName, resourceManagerIdentifier, managedIdentifier, out nodeNameMatches, out whereabouts, out resourceManagerShim);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ public static TimeSpan MaximumTimeout
}
}

public static bool ImplicitDistributedTransactions { get; set; }

// This routine writes the "header" for the recovery information, based on the
// type of the calling object and its provided parameter collection. This information
// we be read back by the static Reenlist method to create the necessary transaction
Expand Down
18 changes: 17 additions & 1 deletion src/libraries/System.Transactions.Local/tests/OleTxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ private static void Test(Action action)
return;
}

TransactionManager.ImplicitDistributedTransactions = true;

// In CI, we sometimes get XACT_E_TMNOTAVAILABLE; when it happens, it's typically on the very first
// attempt to connect to MSDTC (flaky/slow on-demand startup of MSDTC), though not only.
// This catches that error and retries.
Expand Down Expand Up @@ -524,7 +526,20 @@ public class OleTxFixture
// this is likely due to on-demand slow startup of MSDTC. Perform pre-test connecting with retry
// to ensure that MSDTC is properly up when the first test runs.
public OleTxFixture()
=> Test(() =>
{
Test(() =>
{
TransactionManager.ImplicitDistributedTransactions = false;
using var tx = new CommittableTransaction();
Assert.Throws<NotSupportedException>(()
=> tx.EnlistDurable(Guid.NewGuid(), new TestEnlistment(Phase1Vote.Done, EnlistmentOutcome.Committed), EnlistmentOptions.None));
});

TransactionManager.ImplicitDistributedTransactions = true;

Test(() =>
{
using var tx = new CommittableTransaction();
Expand All @@ -536,5 +551,6 @@ public OleTxFixture()
tx.Commit();
});
}
}
}

0 comments on commit 198054d

Please sign in to comment.