documentService.load does not return the up to date document

As a workaround for the async task completion in the new workflows architecture we have an rpc request that gets called after the completeTaskRequest. This rpc has a while loop that executes a documentService.load every 500ms and we check if the updated task metadata is included in the document. I checked if the modify document request that sets the metadata is called. It is correctly called so the data should be up to date after 3 to 4 executions of the timeout. Despite the modify document request being called the load request returns an old document state without the correct metadata. Why could this be the case? Shouldn’t the load request always return the correct document?

ResponseEntity<CompleteTaskResponseDto> waitForMetadata(@RequestBody CompleteTaskDto taskDto) {
        boolean successful = false;
        int currentTry = 0;

        DocumentReference docRef = new DocumentReference(taskDto.getDocRef());

        while(!successful && currentTry < 20) {
            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            Optional<DataServicesDocument> doc = documentService.load(docRef);

            if (doc.isEmpty()) {
                continue;
            }

            Optional<String> taskId = modulFDocumentUtils.getSingleValue(doc.get().getKernelDocument(), "/a12wf/task/id");

            if (taskId.isPresent() && !taskId.get().equals(taskDto.getOldTaskId())) {
                successful = true;
                break;
            }

            Optional<Boolean> isFinished = modulFDocumentUtils.getSingleValue(doc.get().getKernelDocument(), "/root/processMetadata/isFinished");

            if (taskId.isEmpty() && isFinished.isPresent() && isFinished.get()) {
                successful = true;
                break;
            }

            currentTry = currentTry + 1;
        }

        return new ResponseEntity<>(CompleteTaskResponseDto.builder().successful(successful).build(), HttpStatus.OK);
    }

This is the correct code I am using to load the documents.

Hi, by default all operations are managed within the same transaction. This means that the document entity should be cached by Hibernate.

This could be the root cause of your problem. Could u please try to create a new transaction when you load the document?

Hi @kien-crisp-oak, @stefan-grey-canyon suspected something similar. We have for now changed the rpc requests so that they get called from the frontend instead of the backend which fixed the problems for now.