3

For spring boot + hibernate I know the session is created from sessionFactory and from the session we create transaction objects and do any DB operation like save(), fetch() and do the commit and close the session.
Does spring boot open session for every DB query and closes it after the operation?

Nishant Bhardwaz
  • 924
  • 8
  • 21
  • Please ask one question per post. – Jens Schauder Jun 04 '21 at 06:39
  • @JensSchauder I agree with this point, but in my opinion, these are an interrelated thing and the answer would touch these 3 corner that's why I just mentioned these 3 question in a single post – Nishant Bhardwaz Jun 04 '21 at 07:23
  • There are tons of related questions here. Each question has very likely a duplicate here on SO, but for each it is a different one, because they are different questions. – Jens Schauder Jun 04 '21 at 07:43

1 Answers1

2

Spring Boot uses by default the "Open Session In View" (anti) pattern. There is an excellent explanation of it and it's problems by Vlad Mihalcea.

Without that sessions are bound to transactions and the transactions are normally defined by @Transactional annotations. You can also use explicit transaction control.

If you don't declare any transactions explicitly, the standard Spring Data JPA repositories methods open a transaction and close it at the end of the method call.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348