Skip to content

EntityFrameworkCore optimistic concurrency support for saga#1707

Merged
jeremydmiller merged 1 commit intoJasperFx:mainfrom
JMounier:main
Oct 13, 2025
Merged

EntityFrameworkCore optimistic concurrency support for saga#1707
jeremydmiller merged 1 commit intoJasperFx:mainfrom
JMounier:main

Conversation

@JMounier
Copy link
Contributor

@JMounier JMounier commented Sep 18, 2025

This PR adds support for optimistic concurrency when a saga is persisted using the EFCore integration (implementation based on concurrency token cf. Handling Concurrency Conflicts). It patches the code generation to:

  • Ask EF if the saga entity has any pending changes and if so increment the internal saga version number.
  • Throw a SagaConcurrencyException when SaveChangesAsync throws a DbUpdateConcurrencyException impacting the actual saga entity.
  • Documentation was updated to recommend flagging the Version property as a concurrency token.

P.S. I tried running the ./build.sh to test my change but I have the whole HttpTests suite failing for unclear reasons and CodegenPreviewCommand failing to compile with the following error:

Method 'get_LockReleaseBehavior' in type 'Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal.NpgsqlHistoryRepository' from assembly 'Npgsql.EntityFrameworkCore.PostgreSQL

writer.WriteComment("Only intercepts concurrency error on the saga itself");

writer.WriteLine($"BLOCK:if (error.Entries.Any(e => e.Entity == ${_saga.Usage})");
writer.WriteLine($"throw new SagaConcurrencyException($\"Saga of type {_saga.VariableType.FullNameInCode()} and id {{ {SagaChain.SagaIdVariableName} }} cannot be updated because of optimistic concurrency violations\");");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using SagaChain.SagaIdVariableName seems like an oversight but I don't have access to the "Variable sagaId" from neither CommitUnitOfWorkFrame nor DetermineUpdateFrame.

@JMounier JMounier force-pushed the main branch 2 times, most recently from dbc0164 to 1d60f9b Compare September 18, 2025 10:02
@JMounier JMounier changed the title Draft: EntityFrameworkCore optimistic concurrency support for saga EntityFrameworkCore optimistic concurrency support for saga Sep 18, 2025
@JMounier JMounier marked this pull request as ready for review September 18, 2025 12:24
@jeremydmiller
Copy link
Member

@JMounier Hey, I did the trick of pulling in the PR, then running the tests. Next time your shop tries a pull request, just make sure to run the test suite locally please? It's 95% there, but the codegen was off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants