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 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
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ static OutboxPersister CreateOutboxPersister(IConnectionManager connectionManage
ISqlOutboxTransaction transactionFactory()
{
return transactionScopeMode
? new TransactionScopeSqlOutboxTransaction(concurrencyControlStrategy, connectionManager, IsolationLevel.ReadCommitted)
? new TransactionScopeSqlOutboxTransaction(concurrencyControlStrategy, connectionManager, IsolationLevel.ReadCommitted, TimeSpan.Zero)
: new AdoNetSqlOutboxTransaction(concurrencyControlStrategy, connectionManager, System.Data.IsolationLevel.ReadCommitted);
}

Expand Down
2 changes: 1 addition & 1 deletion src/SqlPersistence.Tests/Outbox/OutboxPersisterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ OutboxPersister Setup(string theSchema)
}

return transactionScope
? new TransactionScopeSqlOutboxTransaction(behavior, connectionManager, IsolationLevel.ReadCommitted)
? new TransactionScopeSqlOutboxTransaction(behavior, connectionManager, IsolationLevel.ReadCommitted, TimeSpan.Zero)
: new AdoNetSqlOutboxTransaction(behavior, connectionManager, System.Data.IsolationLevel.ReadCommitted);
},
cleanupBatchSize: 5);
Expand Down
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,23 @@ 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)
{
if (timeout > TransactionManager.MaximumTimeout)
{
throw new Exception("Timeout requested is longer than the maximum value for this machine. Override using the maxTimeout setting of the system.transactions section in machine.config");
}
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)
{
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 // TimeSpan.Zero is default of `TransactionOptions.Timeout`
};

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