Skip to content

Commit

Permalink
MassTransit#5. Fixing dependencies on SqlLockStatementProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
matei-tm committed Sep 8, 2022
1 parent 5b0f207 commit e15d892
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/SampleBatch.Components/CustomSqlLockStatementFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using MassTransit.EntityFrameworkCoreIntegration;
using System.Text;

namespace SampleBatch.Components
{
internal class CustomSqlLockStatementFormatter : ILockStatementFormatter
{
public void Create(StringBuilder sb, string schema, string table)
{
sb.AppendFormat("SELECT * FROM [{0}].{1} WITH (UPDLOCK, ROWLOCK, SERIALIZABLE) WHERE ", schema, table);
}

public void AppendColumn(StringBuilder sb, int index, string columnName)
{
if (index == 0)
sb.AppendFormat("{0} = @p0", columnName);
else
sb.AppendFormat(" AND {0} = @p{1}", columnName, index);
}

public void Complete(StringBuilder sb)
{
}

public void CreateOutboxStatement(StringBuilder sb, string schema, string table, string columnName)
{
sb.AppendFormat(@"SELECT TOP 1 * FROM [{0}].{1} WITH (UPDLOCK, ROWLOCK, READPAST) ORDER BY {2}", schema, table, columnName);
}
}
}
4 changes: 2 additions & 2 deletions src/SampleBatch.Components/CustomSqlLockStatementProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class CustomSqlLockStatementProvider :
{
const string DefaultSchemaName = "dbo";

public CustomSqlLockStatementProvider(string lockStatement)
: base(DefaultSchemaName, lockStatement)
public CustomSqlLockStatementProvider()
: base(DefaultSchemaName, new CustomSqlLockStatementFormatter())
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public BatchStateMachineTests()
db.Database.EnsureCreated();
}

_sagaRepository = EntityFrameworkSagaRepository<BatchState>.CreatePessimistic(_dbContextFactory, new CustomSqlLockStatementProvider("select * from {0}.{1} WITH (UPDLOCK, ROWLOCK) WHERE BatchId = @p0"));
_sagaRepository = EntityFrameworkSagaRepository<BatchState>.CreatePessimistic(_dbContextFactory, new CustomSqlLockStatementProvider());
_stateMachine = new BatchStateMachine();

_inMemoryTestHarness = new InMemoryTestHarness();
Expand Down

0 comments on commit e15d892

Please sign in to comment.