SimplCommerce icon indicating copy to clipboard operation
SimplCommerce copied to clipboard

MySqlRetryingExecutionStrategy

Open rodriguez90 opened this issue 4 years ago • 4 comments

Hello community, I have the follo error at .develop custom payment provider. When insert one order fail de transaction in the order service.

Call Order Service from payment provider image

Order Service File

image

Error

image

rodriguez90 avatar Apr 20 '20 20:04 rodriguez90

does this issue is fixed ? should I work on this issue

Abhi441 avatar Oct 24 '23 18:10 Abhi441

Seems it's still there

hishamco avatar Oct 26 '23 05:10 hishamco

I solved this issue with

await ResilientTransaction.New(db).ExecuteAsync(async () => { _orderRepository.SaveChanges(); await PublishOrderCreatedEvent(order); foreach (var subOrder in subOrders) { await PublishOrderCreatedEvent(subOrder); }

            _couponService.AddCouponUsage(cart.CustomerId, order.Id, checkingDiscountResult);
            _orderRepository.SaveChanges();
        });
        
        public class ResilientTransaction
{
    private DbContext _context;
    private ResilientTransaction(DbContext context) =>
        _context = context ?? throw new ArgumentNullException(nameof(context));

    public static ResilientTransaction New(DbContext context) =>
        new ResilientTransaction(context);

    public async Task ExecuteAsync(Func<Task> action)
    {
        // Use of an EF Core resiliency strategy when using multiple DbContexts
        // within an explicit BeginTransaction():
        // https://docs.microsoft.com/ef/core/miscellaneous/connection-resiliency
        var strategy = _context.Database.CreateExecutionStrategy();
        await strategy.ExecuteAsync(async () =>
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                await action();
                transaction.Commit();
            }
        });
    }
}

rodriguez90 avatar Oct 27 '23 18:10 rodriguez90

Could you please submit a PR?

hishamco avatar Oct 28 '23 15:10 hishamco