Saša Šijak
Saša Šijak

Reputation: 9331

Is Entity Manager cleared automatically after each request?

Spring is providing one Entity Manager per thread. But I can`t find info if Spring clears Entity Manager after @RestControllers method is finished executing? So for example, if I have a method similar to this

    @GetMapping("/{id}")
    public ResponseEntity<SomeEntity> someRequest() {
        SomeEntity res = someService.doSomeJpaRelatedWork();
        return new ResponseEntity<>(res), HttpStatus.OK);

    }

Will spring call EntityManager.clear() after the request or will Entity Manager keep entities for further requests on that thread?

Upvotes: 0

Views: 590

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81998

Since your method doesn't use an EntityManager nor has it @Transactional annotation it is completely independent of the EntityManager and will on its own not affect any EntityManager.

Also, I doubt anything is Spring will call clear implicitly.

BUT Spring doesn't use one EntityManager per Thread but one per request. So the next request in your web application will get a fresh EntityManager with a clear 1st level cache. So while the correct answer for the question you asked is "No, clear isn't called", the answer which is probably relevant is "Yes, the EntityManager is clear on each call of your controller method."

Upvotes: 1

Related Questions