Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions DbaClientX.Core/DatabaseClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
#if NET472 || NET8_0_OR_GREATER || NET5_0_OR_GREATER || NET6_0_OR_GREATER || NETCOREAPP3_0_OR_GREATER
using System.Transactions;
#endif
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_0_OR_GREATER
using System.Runtime.CompilerServices;
#endif
Expand Down Expand Up @@ -167,6 +170,61 @@ private TimeSpan ComputeBackoffDelay(int attempt)
return TimeSpan.FromMilliseconds(total);
}

/// <summary>
/// Attempts to enlist the provided connection in an ambient distributed transaction when available.
/// </summary>
/// <param name="connection">Connection to enlist.</param>
/// <returns><see langword="true"/> when the enlistment succeeded; otherwise <see langword="false"/>.</returns>
protected virtual bool TryEnlistInAmbientTransaction(DbConnection connection)
{
#if NET472 || NET8_0_OR_GREATER || NET5_0_OR_GREATER || NET6_0_OR_GREATER || NETCOREAPP3_0_OR_GREATER
var ambient = Transaction.Current;
if (ambient == null)
{
return false;
}

try
{
connection.EnlistTransaction(ambient);
return true;
}
catch (PlatformNotSupportedException)
{
return false;
}
catch (NotSupportedException)
{
return false;
}
catch (TransactionException)
{
return false;
}
#else
_ = connection;
return false;
#endif
}

/// <summary>
/// Asynchronously attempts to enlist the provided connection in an ambient distributed transaction when available.
/// </summary>
/// <param name="connection">Connection to enlist.</param>
/// <param name="cancellationToken">Token used to cancel the enlistment attempt.</param>
/// <returns><see langword="true"/> when the enlistment succeeded; otherwise <see langword="false"/>.</returns>
protected virtual Task<bool> TryEnlistInAmbientTransactionAsync(DbConnection connection, CancellationToken cancellationToken = default)
{
#if NET472 || NET8_0_OR_GREATER || NET5_0_OR_GREATER || NET6_0_OR_GREATER || NETCOREAPP3_0_OR_GREATER
_ = cancellationToken;
return Task.FromResult(TryEnlistInAmbientTransaction(connection));
#else
_ = connection;
_ = cancellationToken;
return Task.FromResult(false);
#endif
}

/// <summary>
/// Adds parameters created from dictionaries of values, types, and directions to the supplied command.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions DbaClientX.Core/DbaClientX.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net472'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.8" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
<Reference Include="System.Transactions" />
</ItemGroup>
<ItemGroup>
<Using Include="System.Collections" />
<Using Include="System.Threading.Tasks" />
Expand Down
Loading
Loading