Tutorials  /  JavaScript

Hibernate Caching - First Level Cache Tutorial

Ccentron Redaktion · November 2024 ·5 min read ·JavaScript, Tutorial

In this tutorial, we explore the powerful Hibernate caching mechanism, focusing on the First Level Cache. Learn how Hibernate optimizes database interactions, reduces query execution times, and improves application performance. We'll dive into practical examples, examine key concepts, and highlight important points to help you make the most of Hibernate caching in your projects.

Hibernate Caching

Hibernate Cache can be very useful in gaining fast application performance if used correctly. The idea behind cache is to reduce the number of database queries, hence reducing the throughput time of the application. Hibernate comes with different types of Cache:

First Level Cache

Hibernate first level cache is associated with the Session object. Hibernate first level cache is enabled by default and there is no way to disable it. However hibernate provides methods through which we can delete selected objects from the cache or clear the cache completely. Any object cached in a session will not be visible to other sessions and when the session is closed, all the cached objects will also be lost.

Second Level Cache

Hibernate Second Level cache is disabled by default but we can enable it through configuration. Currently EHCache and Infinispan provides implementation for Hibernate Second level cache and we can use them. We will look into this in the next tutorial for hibernate caching.

Query Cache

Hibernate can also cache result set of a query. Hibernate Query Cache doesn’t cache the state of the actual entities in the cache; it caches only identifier values and results of value type. So it should always be used in conjunction with the second-level cache.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Hibernate Caching - First Level Cache Example

For my hibernate first level cache example program, I am using same configuration as in HQL Example, you can check that out and configure the tables and populate it with dummy data. Let’s first look into the program, it’s output and then we will go through some of the important points related to Hibernate First Level Cache.

Go

package com.journaldev.hibernate.main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.journaldev.hibernate.model.Employee;
import com.journaldev.hibernate.util.HibernateUtil;

public class HibernateCacheExample {

    public static void main(String[] args) throws InterruptedException {
        
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();
        
        //Get employee with id=1
        Employee emp = (Employee) session.load(Employee.class, new Long(1));
        printData(emp,1);
        
        //waiting for sometime to change the data in backend
        Thread.sleep(10000);
        
        //Fetch same data again, check logs that no query fired
        Employee emp1 = (Employee) session.load(Employee.class, new Long(1));
        printData(emp1,2);
        
        //Create new session
        Session newSession = sessionFactory.openSession();
        //Get employee with id=1, notice the logs for query
        Employee emp2 = (Employee) newSession.load(Employee.class, new Long(1));
        printData(emp2,3);
        
        //START: evict example to remove specific object from hibernate first level cache
        //Get employee with id=2, first time hence query in logs
        Employee emp3 = (Employee) session.load(Employee.class, new Long(2));
        printData(emp3,4);
        
        //evict the employee object with id=1
        session.evict(emp);
        System.out.println("Session Contains Employee with id=1?"+session.contains(emp));

        //since object is removed from first level cache, you will see query in logs
        Employee emp4 = (Employee) session.load(Employee.class, new Long(1));
        printData(emp4,5);
        
        //this object is still present, so you won't see query in logs
        Employee emp5 = (Employee) session.load(Employee.class, new Long(2));
        printData(emp5,6);
        //END: evict example
        
        //START: clear example to remove everything from first level cache
        session.clear();
        Employee emp6 = (Employee) session.load(Employee.class, new Long(1));
        printData(emp6,7);
        Employee emp7 = (Employee) session.load(Employee.class, new Long(2));
        printData(emp7,8);
        
        System.out.println("Session Contains Employee with id=2?"+session.contains(emp7));
        
        tx.commit();
        sessionFactory.close();
    }

    private static void printData(Employee emp, int count) {
        System.out.println(count+":: Name="+emp.getName()+", Zipcode="+emp.getAddress().getZipcode());
    }
}

When we run above example, the output contains a lot of hibernate related information. But we are mostly interested in our code specific output and the queries fired by hibernate to load the data. The output snippet looks like below.

Code

Hibernate Configuration loaded
Hibernate serviceRegistry created
Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.emp_salary as emp_sala3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.address_line1 as address_2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from EMPLOYEE employee0_ left outer join ADDRESS address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
1:: Name=Pankaj, Zipcode=95129
2:: Name=Pankaj, Zipcode=95129
Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.emp_salary as emp_sala3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.address_line1 as address_2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from EMPLOYEE employee0_ left outer join ADDRESS address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
3:: Name=PankajK, Zipcode=95129

First Level Cache in Hibernate Important Points

Important Points about First level cache in Hibernate that can be derived from above program are:

  • Hibernate First Level cache is enabled by default, there are no configurations needed for this.
  • Hibernate first level cache is session specific, that’s why when we are getting the same data in same session there is no query fired whereas in other session query is fired to load the data.
  • Hibernate first level cache can have old values, as you can see above that I have put my program to sleep for 10 seconds and in that time I updated the value (name from Pankaj to PankajK) in database but it didn’t get reflected in the same session. But in other session, we got the updated value.
  • We can use session evict() method to remove a single object from the hibernate first level cache.
  • We can use session clear() method to clear the cache i.e delete all the objects from the cache.
  • We can use session contains() method to check if an object is present in the hibernate cache or not, if the object is found in cache, it returns true or else it returns false.
  • Since hibernate cache all the objects into session first level cache, while running bulk queries or batch updates it’s necessary to clear the cache at certain intervals to avoid memory issues.

That’s all for hibernate caching and first level cache example, in future posts we will look into Hibernate Second Level Cache - EHCache implementation.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren