I am currently working on an application that uses Spring Boot and Spring Data (its JpaRepository
interfaces to be precise) together with Hibernate.
One thing I love about Hiberante is its caching feature - when you submit multiple queries that match a particular object, you will get back the same instance of that object on every query execution (with respect to Java's == operator). However, when using Spring Data and JpaRepository
classes, this does not always seem to be the case. For that reason, I assume that there are multiple HibernateSession
instances at work here.
My question therefore is: how does Spring Data handle Hibernate Sessions? When does it open or close them? Is there a way to configure it to use the same session for the entire runtime of my application to make full use of Hibernate's object cache? Is there a reason not to do it that way?
Thanks,
Alan
Spring Boot is an open source Java-based framework used to create a micro Service. It is developed by Pivotal Team and is used to build stand-alone and production ready spring applications.
Spring Boot is a lightweight framework that takes most of the work out of configuring Spring-based applications. In this tutorial, you'll learn how to use Spring Boot's starters, opinions, and executable JAR file structure to quickly create Spring-based applications that "just run."
Spring Boot is a backend framework that has become a major player in the enterprise Java ecosystem. It lets Java developers start building web applications quickly, without fuss.
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications. It's a Java-based framework used to create a microservice ( microservice is defined as the small services that work together.
I think I've found the answer myself. If somebody finds this question, here's my answer.
How does Spring manage Hibernate Sessions?
By default, Spring Boot applies transaction management at the repository level. In this case, when calling a JpaRepository
method (or in general any Repository
method), Spring will:
SessionFactory
to create a new sessionRepository
methodHowever, if you apply @Transactional
to the service class or method, Spring will open the session and the transaction on entry to the service method, and the repository method will be performed within the existing transaction.
What are the consequences?
As a programmer...
@Transactional
on a larger scope than the repository. Caching only works within the same HibernateSession
.@Entity
objects by their Hibernate ID value, rather than by using Java's ==
operator.@OneToMany
reference) in your @Entity
classes (see FetchMode.LAZY
as opposed to FetchMode.EAGER
) are used exclusively within an @Transactional
-annotated methodAlso for reference, the following link has been quite helpful: Multiple Transactions in single session
As with many other aspects of Spring, there is a lot to be gained here, if you are willing to sacrifice direct control over your application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With