Otvoriť Uzavreté

Error on UnitOfWork in exception #7216


User avatar
0
andmattia vytvorené
  • 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 odpoveď(e)/dí
  • User Avatar
    0
    maliming vytvorené
    Tím podpory 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 vytvorené

    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 vytvorené
    Tím podpory 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 v8.2.0-preview Updated on marca 25, 2024, 15:11