Base solution for your next web application
Open Closed

Updating database record in Finally block after exception #7066


User avatar
0
huntethan89 created
public void Execute(int Id) 
{
    string status = "";
    try {
        .............
        .............

        status = "Succeeded";
    }
    catch (Exception ex) {
        status = "Failed";
    } 
    finally {
      _appService.Update(Id, status);
    }
}

Update method -

public void Update(int Id, string status)
{
    var record = _repository.Get(Id);
    record.LastRunStatus = status;

    _repository.Update(record);
    CurrentUnitOfWork.SaveChanges();
}

The record is only updates if there is no exception in Execute method. I want the record to update even when there is exception. How can we achieve this?


1 Answer(s)
  • User Avatar
    1
    maliming created
    Support Team

    Try using a new unit of work and transaction.

    using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequireNew))
    {
    	//Insert or Update..
    
    	unitOfWork.Complete();
    }