简体   繁体   English

Hangfire,.Net Core和实体框架:并发异常

[英]Hangfire, .Net Core and Entity Framework: concurrency exception

I am developing a .Net core application with Hangfire and facing the below exception 我正在使用Hangfire开发.Net核心应用程序,并且遇到以下异常

A second operation started on this context before a previous operation completed. 在先前的操作完成之前,第二操作在此上下文上开始。 Any instance members are not guaranteed to be thread safe. 不保证任何实例成员都是线程安全的。

I have used Hangfire for scheduling the jobs with 1 hour interval. 我已经使用Hangfire安排工作间隔为1小时。 I am facing the above issue when the new process/job gets started before the earlier job has finished its process. 当新的流程/作业在较早的作业完成其流程之前开始时,我面临上述问题。

How can we implement multiple Hangfire processes/jobs(multiple workers) to work (in parallel) to accomplish the task. 我们如何实现多个Hangfire流程/职位(多个工人)来工作(并行)以完成任务。 (Resolved now, by using the default AspNetCoreJobActivator) (现在已解决,使用默认的AspNetCoreJobActivator)

var scopeFactory = serviceProvider.GetService<IServiceScopeFactory>();
            if (scopeFactory != null)
                GlobalConfiguration.Configuration.UseActivator(new AspNetCoreJobActivator(scopeFactory));

Now, I am getting the following exception in CreateOrderData.cs:- 现在,我在CreateOrderData.cs中收到以下异常:-

/*System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. /*System.InvalidOperationException:引发了一个异常,该异常可能是由于瞬时故障所致。 If you are connecting to a SQL Azure database consider using SqlAzureExecutionStrategy. 如果要连接到SQL Azure数据库,请考虑使用SqlAzureExecutionStrategy。 ---> Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. ---> Microsoft.EntityFrameworkCore.DbUpdateException:更新条目时发生错误。 See the inner exception for details. 有关详细信息,请参见内部异常。 ---> System.Data.SqlClient.SqlException: Transaction (Process ID 103) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. ---> System.Data.SqlClient.SqlException:事务(进程ID 103)已与另一个进程在锁资源上死锁,并且已被选择为死锁受害者。 Rerun the transaction. 重新运行事务。 */ * /

I am scheduling the hangfire cron job as below:- 我正在按如下方式安排hangfire cron工作:

RecurringJob.AddOrUpdate<IS2SScheduledJobs>(x => x.ProcessInputXML(), Cron.MinuteInterval(1));

Startup.cs Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    string hangFireConnection = Configuration["ConnectionStrings:HangFire"];
    GlobalConfiguration.Configuration.UseSqlServerStorage(hangFireConnection);

    var config = new AutoMapper.MapperConfiguration(cfg =>
    {
       cfg.AddProfile(new AutoMapperProfileConfiguration());
    );

    var mapper = config.CreateMapper();
    services.AddSingleton(mapper);

    services.AddScoped<IHangFireJob, HangFireJob>();
    services.AddScoped<IScheduledJobs, ScheduledJobs>();
    services.AddScoped<BusinessLogic>();
    services.AddHangfire(opt => 
         opt.UseSqlServerStorage(Configuration["ConnectionStrings:HangFire"]));

    services.AddEntityFrameworkSqlServer().AddDbContext<ABCContext>(options => 
         options.UseSqlServer(Configuration["ConnectionStrings:ABC"]));
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
{
    GlobalConfiguration.Configuration.UseActivator(new HangFireActivator(serviceProvider));

    //hangFireJob.Jobs();

    // add NLog to ASP.NET Core
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();
    loggerFactory.AddNLog();
    // app.UseCors("AllowSpecificOrigin");

    foreach (DatabaseTarget target in LogManager.Configuration.AllTargets.Where(t => t is DatabaseTarget))
    {
        target.ConnectionString = Configuration.GetConnectionString("Logging");
    }

    LogManager.ReconfigExistingLoggers();
}

Hangfire.cs Hangfire.cs

public class HangFireJob : IHangFireJob
{
        private ABCContext _abcContext;
        private IScheduledJobs scheduledJobs;

        public HangFireJob(ABCContext abcContext, IScheduledJobs scheduledJobs)
        {
            _abcContext = abcContext;
            this.scheduledJobs = scheduledJobs;           
        }

        public void Jobs()
        {
             RecurringJob.AddOrUpdate<IScheduledJobs>(x => x.ProcessInputXML(), Cron.HourInterval(1));
        }
}

ScheduledJobs.cs ScheduledJobs.cs

public class S2SScheduledJobs : IS2SScheduledJobs
{
    private BusinessLogic _businessLogic;

    public ScheduledJobs(BusinessLogic businessLogic)
    {
        _businessLogic = businessLogic;
    }

    public async Task<string> ProcessInputXML()
    {
        await _businessLogic.ProcessXML();
    }
}

BusinessLogic.cs BusinessLogic.cs

public class BusinessLogic
{
    private ABCContext _abcContext;

    public BusinessLogic(ABCContext abcContext) : base(abcContext)
    {
            _abcContext = abcContext;
    }

    public async Task ProcessXML()
    {
       var batchRepository = new BatchRepository(_abcContext);
       var unprocessedBatchRecords = await BatchRepository.GetUnprocessedBatch();

       foreach (var batchRecord in unprocessedBatchRecords)
       {
         try
         {
           int orderId = await LoadDataToOrderTable(batchRecord.BatchId);  
           await UpdateBatchProcessedStatus(batchRecord.BatchId);

           if (orderId > 0)
           {
                await CreateOrderData(orderId);
           }
         }
         catch(Exception ex)
         {
         }
       }
    }

CreateOrderData.cs CreateOrderData.cs

public async Task<int> CreateOrderData(int orderId)
{
  try
  {
    await OrderRepo.InsertOrder(order);
    await _abcContext.SaveChangesAsync();   
  }
  catch(Exception ex)
  {
    /*System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. If you are connecting to a SQL Azure database consider using SqlAzureExecutionStrategy. ---> Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Transaction (Process ID 103) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction. */ 
  }
}

InsertOrder.cs InsertOrder.cs

public async Task InsertOrder(Order o)
{
   // creation of large number of entites(more than 50) to be inserted in the database
    woRepo.Insert(p);
    poRepo.Insert(y);
 //and many more like above

    Insert(order);
}

Insert.cs Insert.cs

public virtual void Insert(TEntity entity)
    {
        entity.ObjectState = ObjectState.Added;
        if (entity is IXYZEntity xyzEntity)
        {
            xyzEntity.CreatedDate = DateTime.Now;
            xyzEntity.UpdatedDate = xyzEntity.CreatedDate;
            xyzEntity.CreatedBy = _context.UserName ?? string.Empty;
            xyzEntity.UpdatedBy = _context.UserName ?? string.Empty;
        }
        else if (entity is IxyzEntityNull xyzEntityNull)
        {
            xyzEntityNull.CreatedDate = DateTime.Now;
            xyzEntityNull.UpdatedDate = xyzEntityNull.CreatedDate;
            xyzEntityNull.CreatedBy = _context.UserName;
            xyzEntityNull.UpdatedBy = _context.UserName;
        }
        _dbSet.Add(entity);
        _context.SyncObjectState(entity);
    }

LoadDataToOrder.cs LoadDataToOrder.cs

public async Task<int> LoadDataToOrder(int batchId)
{
        //  using (var unitOfWork = new UnitOfWork(_abcContext))
        //  {
        var orderRepo = new OrderRepository(_abcContext);
        Entities.Order order = new Entities.Order();

        order.Guid = Guid.NewGuid();
        order.BatchId = batchId;
        order.VendorId = null;

        orderRepo.Insert(order);
        //unitOfWork.SaveChanges();
        await _abcContext.SaveChangesAsync();
        return order.OrderId;
        //  
}
}

HangfireActivator.cs HangfireActivator.cs

public class HangFireActivator : Hangfire.JobActivator
{
        private readonly IServiceProvider _serviceProvider;

        public HangFireActivator(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public override object ActivateJob(Type type)
        {
            return _serviceProvider.GetService(type);
        }
}

Please advise. 请指教。

Thanks. 谢谢。

Following solutions worked for the 2 problems: 以下解决方案可解决这两个问题:

  1. Implementation of multiple Hangfire processes/jobs(multiple workers) to work (in parallel). 实施多个Hangfire流程/工作(多个工人)(并行)。 Answer: This got resolved when I used the built-in AspNetCoreJobActivator instead that's available out of the box, ie removed the HangfireActivator class and removed the call to the UseActivator method. 答:当我使用内置的AspNetCoreJobActivator时,此问题得到解决,而该内置的现成可用的,即删除了HangfireActivator类并删除了对UseActivator方法的调用。

     var scopeFactory = serviceProvider.GetService<IServiceScopeFactory>(); if (scopeFactory != null) GlobalConfiguration.Configuration.UseActivator(new AspNetCoreJobActivator(scopeFactory)); 
  2. SqlAzureExecutionStrategy Exception in CreateOrder.cs (transaction was deadlocked) CreateOrder.cs SqlAzureExecutionStrategy异常(事务已死锁)

Answer: Resolved this issue by retrying the query automatically when deadlock occurs. 答:通过在发生死锁时自动重试查询来解决此问题。

Thanks odinserj for the suggestions. 感谢odinserj的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM