Starts in:
2 DAYS
11 HRS
5 MIN
18 SEC
Starts in:
2 D
11 H
5 M
18 S
Open Closed

Error on UnitOfWork in exception #7216


User avatar
0
andmattia created
  • ABP Framework version: v7.4.5
  • UI Type: Angular
  • Database System: PostgreSQL
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes

I've an handler to sync data from microservices

 public async Task HandleEventAsync(ProductEto eventData)
    {
        if (eventData != null)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin())
            {
                _currentTenant.Change(eventData.TenantId);
                try
                {
                    var gmd = _objectMapper.Map<ProductEto, ProductSync>(eventData);

                    await _productSyncRepository.InsertAsync(gmd);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, "Handle Event ProductEto Error");
                }
                finally
                {
                    await unitOfWork.CompleteAsync();
                }
            }
        }
        else
        {
            Logger.LogWarning("Input data is null into handle event ProductEto");
        }
    }

In this case I've a PK duplicate so Postgres raise an excetion but this happen in finally and my handler continue to retry process my data.

Also I observe that if I call InsertAsyc/UpdateAsync and not return to a local varibile update or insert in some case does't work, if I return the value it always work.


3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can try the code below

    
    public async Task HandleEventAsync(ProductEto eventData)
    {
        if (eventData != null)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin(requiresNew: true))
            {
                _currentTenant.Change(eventData.TenantId);
                try
                {
                    var gmd = _objectMapper.Map<ProductEto, ProductSync>(eventData);
    
                    await _productSyncRepository.InsertAsync(gmd);
                    await unitOfWork.CompleteAsync();
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, "Handle Event ProductEto Error");
                }
            }
        }
        else
        {
            Logger.LogWarning("Input data is null into handle event ProductEto");
        }
    }
    
  • User Avatar
    0
    andmattia created

    Hi

    I'll try but I think it works beacuse task raise exception inside the try but is this the best approch to use UOW in using?

    About Insert/Update issue any idea?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    If there is an unhandled exception the uow should fall back/fail.

    The InsertAsyc/UpdateAsync method has an autoSave parameter. You can set it to true.

    https://docs.abp.io/en/abp/latest/Unit-Of-Work#alternative-to-the-savechanges

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06