Search code examples
javahibernatejpapersistencequarkus

Unable to persist entity with managed executor in Quarkus


I am unable to persist an entity using ManagedExecutor in Quarkus.

managedExecutor.runAsync(() -> {
                    User user = User.findById(userId);
                    if (user == null) {
                        return;
                    }
                    BigDecimal amount;
                    try {
                        amount = new BigDecimal(split[1]);
                    } catch (NumberFormatException e) {
                        throw new BadRequestException("Invalid amount: " + split[1] + ", " + e.getMessage());
                    }
                    managedExecutor.runAsync(threadContext.contextualRunnable(new Runnable() {
                        @Override
                        @Transactional
                        @ActivateRequestContext
                        public void run() {
                            user.minimumAmount = amount;
                            user.persistAndFlush();
                        }
                    }));
                    sendMessage(userId, new NewMinimumAmountMessage(user.minimumAmount));
                });

Right now there are no exceptions thrown and nothing after persistAndFlush() gets executed. I have tried to put the persist in the initial runAsync but that also doesn't work.

The code here is in a websocket function which is annotated with @OnMessage, putting @Transactional here doesn't do anything.

Both the ManagedExecutor and the ThreadContext are injected.


Solution

  • I was able to fix it but I know it's not the proper way.

    Response response = given()
                                .auth().oauth2(token)
                                .header("Bearer", token)
                                .header("Content-Type", "application/json")
                                .when().put("/api/user/amount/" + split[1])
                                .thenReturn();
    

    I added a path in a resource and used the test function to call it.