Caching is an import technique in Hibernate. When you use Hibernate in the application and enable Hibernate SQL trace, you might notice lots of SQL queries executed to grab data back from the database. A large number of such kinds of queries will finally slow down your application. Hibernate caching is a good choice to minimize the database hits and improve the performance of the application.
Hibernate cache refers to the area (memory, 3rd part cache server etc.) which stores the data obtained from the database. When the application wants to access some data, it will check the cache before accessing the database. When data is available in the cache, the application will get the data without querying the database. This will significantly minimize the response time.
Hibernate offers two caching levels: The first level cache and the second level cache (shown below). Besides that, Hibernate also provides query cache support.
First level cache
The first level cache is the session cache. It is enabled by default and cannot be disabled.
- Objects are cached within the current session, and available before the session is closed.
- If an entity object is updated multiple times, Hibernate tries to delay the update to reduce the number of update commands to the database.
- When the session is closed, all the objects being cached are lost.
- When an entity is retrieved from database, it will be stored in first level cache. If the same entity object is queried within the same session, it will be loaded from cache without database query.
- Session cache can be cleaned using clear() method. Single entity cache can be removed using evict() method
Second level cache
Second-level cache an optional cache, and always associates with the Session Factory object.
The facts of 2nd level cache:
- second level cache is created in session factory scope and is available to be used in all sessions created using the session factory object.
- When session factory is closed, all cache associated with it will be gone.
- Whenever hibernate session try to load an entity, Hibernate will look for the cached copy of entity in first level cache. If the cached copy is not available in the first level cache, it will search second level cache. If the cached copy can be found in the second level, it will update 1st level cache and return the data. If the entity cannot be found in 1st level cache and 2nd level cache, Hibernate will query database and the returned entity will be saved in both 1st level cache and 2nd level cache.
- Widely used cache implementations include: EhCache, OSCache, SwarmCache, JBoss TreeCache etc. Those implementations can be used as the second level cache provider.
To support second level cache, we need to specify the cache provider. Here is an example of the part of configuration (hibernate.cfg.xml):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
<!-- Use EHCache for cache-->
Query cache does not cache the state of actual entities in the result set, and it caches only identifier values and results of value type. Query cache integrates closely with the second-level cache, and is useful for queries that are executed frequently with the same parameters.
To enable query cache, you can add one line to the hibernate configuration file.
In addition, in your code, you need set your Query cacheable. We will give an example later.
The diagram shows how Hibernate Query Cache works.