Can't Use External Transactions and Retry Strategy with Stored Procedures
See original GitHub issueWith the current way store procedures are wrapped, you can’t include them in an externally defined and managed unit of work. Please refer to “Using Resilient Entity Framework Core Sql Connections and Transactions: Retries with Exponential Backoff”. The article describes how to create an ExecutionStrategy and pass it a delegate as the unit of work to be executed and retries if a transient error occurs. Within the delegate, the transaction is started and committed.
Steps to reproduce
Sorry, I don’t really have steps to reproduce this because it doesn’t seem like it can be done. The core problem is that you need create the ExecutionStrategy on the dbcontext that will be used for the stored procedure. However, this is not exposed in the extensions.
You can sort of hack this in by changing the extensions as shown here:
Then you can do something like this:
using var databaseContext = new DeadlocktestContext();
var strategy = databaseContext.Database.CreateExecutionStrategy();
await strategy.ExecuteAsync(
async () =>
{
using (var transaction = databaseContext.Database.BeginTransaction())
{
await databaseContext.Procedures.Procedure1Async(1);
await transaction.CommitAsync();
}
}
);
However, it seems like a hack or that there should be a better way.
EF Core version in use: EF Core 6.02 Is Handlebars used: no EF Core Power Tools version: 2.5.908.0 Database engine: SQL Server Visual Studio version: Visual Studio 2022
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (7 by maintainers)
Top GitHub Comments
It does solve my issue. Here is what I did:
Example run:
This is the entire test code: DeadlockTest.zip
Not sure how you handle closure of issues. Please close this when you feel it should be closed.
@ErikEJ I tried the latest build and it looks good ! Thanks.