Quarkus asynchronous

Viewed 1070

I try to develop a scenario here my code must be asynchronous using quarkus framework, bellow a snippet of my code:

@Inject
ThreadContext threadContext;

@Inject
ManagedExecutor managedExecutor

@Transactional
@ActivateRequestContext
private void asyncMethod(DataAccessAuthorisationEntity dataAccess) {
    dataAccess.setStatus(IN_PROGRESS);//!!!!!!!!!
    dataAccessAuthorisationRepository.persist(dataAccess);


    threadContext.withContextCapture(CompletableFuture.completedFuture("T")).runAsync(()->{

        logger.info("[][][] for dataAccess id we begin the treatement "+dataAccess.getId());
        boolean exit = false;
        PortfolioEntity portfolioEntity = portfolioRepository.findById(dataAccess.getPortfolioId());
        System.out.println("");
        try {
            logger.info("[BEGIN][copyFileAfterSharing] for data access id= "+dataAccess.getId());
            String portfolioId = portfolioEntity.getExternalId() + "_" + portfolioEntity.getExternalIdType().getCode();
            fileService.copyFileOnAnotherServer(new CopyObject(portfolioId, dataAccess.getStartPoint().toString(),
                    dataAccess.getEmitterOrganisationId(), dataAccess.getRecipientOrganisationId()));
        } catch (Exception e) {
            dataAccess.setStatus(PENDING);//!!!!!!!!!
            dataAccessAuthorisationRepository.persist(dataAccess);
            logger.info("[ERROR][copyFileAfterSharing][BELLOW STACKTRACE] for data access id= "+dataAccess.getId());
            e.printStackTrace();
            exit= true;
        }



    },managedExecutor);
}

but I get always when I my execution pass by the exception catch and When I call dataAccessAuthorisationRepository.persist(dataAccess) I get:

Transaction is not active, consider adding @Transactional to your method to automatically activate one.

because I update my entity dataAccess twice time in the same transaction

1 Answers

Quarkus creates a proxy wrapper around your instance that is injected. When you call a method of a manged bean you call actually this proxy object, that hanldes annotations. If you call a mehtod via "this." the Bean-container/proxy will not detect this call as the call does not go thorugh it. You can't use annotations on calls with "this.".

Related