Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How spring data jpa manages session for every transaction?

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?

like image 255
Nishant Bhardwaz Avatar asked Nov 09 '25 00:11

Nishant Bhardwaz


1 Answers

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.

like image 50
Jens Schauder Avatar answered Nov 12 '25 07:11

Jens Schauder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!