By default Spring boot uses open session in view pattern.
Does it mean that each time method annotated with @Transactional uses the entity manager created at the beginning of the request or creates a new one each time?
CodePudding user response:
@Transactional requires a transaction by default, so it will reuse an already running transaction if possible, or create a new one.
CodePudding user response:
A transaction, unless with REQUIRES_NEW
will re-use an existing thread bound EntityManager
or Session
(depending if you use JPA or plain Hibernate). The open session in view, will open and bind one at the start of a request.
So it will reuse the current thread bound one.
For plain Hibernate you can check the SpringSessionContext
class. For JPA this is a bit hidden in proxies, but you could check the EntityManagerFactoryUtils
class.