Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transaction timeout duration configuration for TransactionOptions in Outbox feature #901

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion src/SqlPersistence/Outbox/SqlOutboxFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected override void Setup(FeatureConfigurationContext context)
adoTransactionIsolationLevel = System.Data.IsolationLevel.ReadCommitted;
}
var transactionScopeIsolationLevel = context.Settings.GetOrDefault<System.Transactions.IsolationLevel>(TransactionScopeIsolationLevel);
var transactionScopeTimeout = context.Settings.GetOrDefault<TimeSpan>(TransactionScopeTimeout);

var outboxCommands = OutboxCommandBuilder.Build(sqlDialect, tablePrefix);

Expand All @@ -44,7 +45,7 @@ protected override void Setup(FeatureConfigurationContext context)
ISqlOutboxTransaction transactionFactory()
{
return transactionScopeMode
? (ISqlOutboxTransaction)new TransactionScopeSqlOutboxTransaction(concurrencyControlStrategy, connectionManager, transactionScopeIsolationLevel)
? (ISqlOutboxTransaction)new TransactionScopeSqlOutboxTransaction(concurrencyControlStrategy, connectionManager, transactionScopeIsolationLevel, transactionScopeTimeout)
: new AdoNetSqlOutboxTransaction(concurrencyControlStrategy, connectionManager, adoTransactionIsolationLevel);
}

Expand Down Expand Up @@ -87,4 +88,5 @@ ISqlOutboxTransaction transactionFactory()
internal const string UseTransactionScope = "Persistence.Sql.Outbox.TransactionScopeMode";
internal const string AdoTransactionIsolationLevel = "Persistence.Sql.Outbox.AdoTransactionIsolationLevel";
internal const string TransactionScopeIsolationLevel = "Persistence.Sql.Outbox.TransactionScopeIsolationLevel";
internal const string TransactionScopeTimeout = "Persistence.Sql.Outbox.TransactionScopeTimeout";
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,19 @@ public static void UseTransactionScope(this OutboxSettings outboxSettings, Isola
outboxSettings.GetSettings().Set(SqlOutboxFeature.UseTransactionScope, true);
outboxSettings.GetSettings().Set(SqlOutboxFeature.TransactionScopeIsolationLevel, isolationLevel);
}

/// <summary>
/// Configures the outbox to use TransactionScope instead of SqlTransaction. This allows wrapping the
/// the outbox transaction (and synchronized storage session it manages) and other database transactions in a single scope - provided that
/// Distributed Transaction Coordinator (DTC) infrastructure is configured.
/// </summary>
/// <param name="outboxSettings">Outbox settings.</param>
/// <param name="isolationLevel">Isolation level to use. Only levels Read Committed, Repeatable Read and Serializable are supported.</param>
/// <param name="timeout">timeout period for the transaction.</param>
public static void UseTransactionScope(this OutboxSettings outboxSettings, IsolationLevel isolationLevel, TimeSpan timeout)
{
outboxSettings.UseTransactionScope(isolationLevel);
outboxSettings.GetSettings().Set(SqlOutboxFeature.TransactionScopeTimeout, timeout);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data.Common;
using System;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using System.Transactions;
Expand All @@ -16,13 +17,15 @@ class TransactionScopeSqlOutboxTransaction : ISqlOutboxTransaction
TransactionScope transactionScope;
Transaction ambientTransaction;
bool commit;
TimeSpan transactionTimeout;

public TransactionScopeSqlOutboxTransaction(ConcurrencyControlStrategy concurrencyControlStrategy,
IConnectionManager connectionManager, IsolationLevel isolationLevel)
IConnectionManager connectionManager, IsolationLevel isolationLevel, TimeSpan transactionTimeout = default)
ramonsmits marked this conversation as resolved.
Show resolved Hide resolved
{
this.connectionManager = connectionManager;
this.isolationLevel = isolationLevel;
this.concurrencyControlStrategy = concurrencyControlStrategy;
this.transactionTimeout = transactionTimeout;
}

public DbTransaction Transaction => null;
Expand All @@ -33,7 +36,8 @@ public void Prepare(ContextBag context)
{
var options = new TransactionOptions
{
IsolationLevel = isolationLevel
IsolationLevel = isolationLevel,
Timeout = transactionTimeout
alexander-kucherov marked this conversation as resolved.
Show resolved Hide resolved
};

transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew, options, TransactionScopeAsyncFlowOption.Enabled);
Expand Down