Home > Software engineering >  Spring boot transaction in open session in view
Spring boot transaction in open session in view

Time:12-24

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.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Transactional.html#propagation--

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.

  • Related