Skip to content

Commit

Permalink
Enable snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasohlund committed Dec 4, 2024
1 parent b2a2047 commit 06c00a6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ jobs:
connection-string-env-var: SQLServerConnectionString
catalog: nservicebus
extra-params: "Encrypt=False;"
- name: Configure snapshot isolation
if: matrix.engine == 'SqlServer'
shell: pwsh
run: |
sqlcmd -Q "ALTER DATABASE nservicebus SET ALLOW_SNAPSHOT_ISOLATION ON" -d "nservicebus"
- name: Setup PostgreSql 15.x
if: matrix.engine == 'PostgreSql'
uses: Particular/setup-postgres-action@v1.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using Newtonsoft.Json;
using NServiceBus.Extensibility;
using NServiceBus.Outbox;
Expand Down Expand Up @@ -36,16 +37,20 @@ static PersistenceTestsConfiguration()
{
var variants = new List<object>();

if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("SQLServerConnectionString")))
var sqlServerConnectionString = Environment.GetEnvironmentVariable("SQLServerConnectionString");
if (!string.IsNullOrWhiteSpace(sqlServerConnectionString))
{
variants.Add(CreateVariant(new SqlDialect.MsSqlServer(),
BuildSqlDialect.MsSqlServer,
supportsDtc: true));

variants.Add(CreateVariant(new SqlDialect.MsSqlServer(),
BuildSqlDialect.MsSqlServer,
usePessimisticMode: true,
supportsDtc: true,
isolationLevel: IsolationLevel.Snapshot));
isolationLevel: IsolationLevel.ReadCommitted));

using var connection = new SqlConnection(sqlServerConnectionString);
connection.Open();
var command = connection.CreateCommand();
command.CommandText = $"ALTER DATABASE {connection.Database} SET ALLOW_SNAPSHOT_ISOLATION ON";
_ = command.ExecuteNonQuery();
}

if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("PostgreSqlConnectionString")))
Expand All @@ -72,8 +77,9 @@ static TestFixtureData CreateVariant(SqlDialect dialect,
bool usePessimisticMode = true,
bool supportsDtc = false,
IsolationLevel isolationLevel = IsolationLevel.ReadCommitted,
bool useTransactionScope = false,
System.Transactions.IsolationLevel scopeIsolationLevel = System.Transactions.IsolationLevel.ReadCommitted) =>
new(new TestVariant(new SqlTestVariant(dialect, buildDialect, usePessimisticMode, supportsDtc, isolationLevel, scopeIsolationLevel)));
new(new TestVariant(new SqlTestVariant(dialect, buildDialect, usePessimisticMode, supportsDtc, isolationLevel, useTransactionScope, scopeIsolationLevel)));

public Task Configure(CancellationToken cancellationToken = default)
{
Expand All @@ -84,6 +90,7 @@ public Task Configure(CancellationToken cancellationToken = default)
var pessimisticMode = variant.UsePessimisticMode;
var isolationLevel = variant.IsolationLevel;
var scopeIsolationLevel = variant.ScopeIsolationLevel;
var useTransactionScopeScope = variant.UseTransactionScope;

if (SessionTimeout.HasValue)
{
Expand All @@ -103,7 +110,7 @@ public Task Configure(CancellationToken cancellationToken = default)
var connectionManager = new ConnectionManager(connectionFactory);
SagaIdGenerator = new DefaultSagaIdGenerator();
SagaStorage = new SagaPersister(infoCache, dialect);
OutboxStorage = CreateOutboxPersister(connectionManager, dialect, false, false, isolationLevel, scopeIsolationLevel);
OutboxStorage = CreateOutboxPersister(connectionManager, dialect, pessimisticMode, useTransactionScopeScope, isolationLevel, scopeIsolationLevel);
SupportsPessimisticConcurrency = pessimisticMode;
CreateStorageSession = () => new StorageSession(connectionManager, infoCache, dialect);

Expand Down
10 changes: 9 additions & 1 deletion src/SqlPersistence.PersistenceTests/SqlTestVariant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class SqlTestVariant(SqlDialect dialect,
bool usePessimisticMode,
bool supportsDtc,
IsolationLevel isolationLevel,
bool useTransactionScope,
System.Transactions.IsolationLevel scopeIsolationLevel)
{
public SqlDialect Dialect { get; } = dialect;
Expand All @@ -20,7 +21,14 @@ class SqlTestVariant(SqlDialect dialect,

public IsolationLevel IsolationLevel { get; } = isolationLevel;

public bool UseTransactionScope { get; } = useTransactionScope;

public System.Transactions.IsolationLevel ScopeIsolationLevel { get; } = scopeIsolationLevel;

public override string ToString() => $"{Dialect.GetType().Name}-pessimistic={UsePessimisticMode}-{IsolationLevel}";
public override string ToString()
{
var mode = UsePessimisticMode ? "pessimistic" : "optimistic";
var transaction = UseTransactionScope ? $"transactionscope({ScopeIsolationLevel})" : $"ado({IsolationLevel})";
return $"{Dialect.GetType().Name}-{mode}-{transaction}";
}
}

0 comments on commit 06c00a6

Please sign in to comment.