Hibernate First Level Cache Example
Let us try to understand the first level cache in hibernate, actually i tried to give almost all the concept about this first level cache hope you will enjoy this ?
- By default, for each hibernate application, the first level cache is automatically been enabled
- As a programmer, we no need to have any settings to enable the first level cache and also we cannot disable this first level cache
- the first level cache is associated with the session object and scope of the cache is limited to one session only
- When we load an object for the first time from the database then the object will be loaded from the database and the loaded object will be stored in the cache memory maintained by that session object
- If we load the same object once again, with in the same session, then the object will be loaded from the local cache memory not from the database
- If we load the same object by opening other session then again the object will loads from the database and the loaded object will be stored in the cache memory maintained by this new session
Example
123456789101112
Session ses1 = factory.openSession();
Object ob1 = ses1.get(Student.class, new Integer(101)); Object ob2 = ses1.get(Student.class, new Integer(101)); Object ob3 = ses1.get(Student.class, new Integer(101)); Object ob4 = ses1.get(Student.class, new Integer(101)); -- session.close();
Session ses2 = factory.openSession();
Object ob5 = ses2.get(Student.class, new Integer(101));
Explanation:
- In line number1, i have opened one session with object is ses1
- In line number2, loaded one object with id 101, now it will loads the object from the database only as its the first time, and keeps this object in the session cache
- See at line number 4,5,6 i tried to load the same object 3 times, but here the object will be loaded from the stored cache only not from the database, as we are in the same session
- In line number 9, we close the first session, so the cache memory related this session also will be destroyed
- See line number 11, again i created one new session and loaded the same object with id 101 in line number 12, but this time hibernate will loads the object from the database
Finally what am trying to tell you is…
x . number of sessions = that many number of cache memories
Important
The loaded objects will be stored in cache memory maintained by a session object and if we want to remove the objects that are stored in the cache memory, then we need to call either evict() or clear() methods. Actually evict() is used to remove a particular object from the cache memory and clear() is to remove all objects in the cache memory
12345678
>Session ses = factory.openSession();
Object ob = ses1.get(Student.class, new Integer(101)); Student s = (Student)ob System.out.println(s.getStudentId());
ses.evict(s);
Object ob1 = ses.get(Student.class, new Integer(101));
- Opened session at line number 1
- Loaded the object with id 101, so hibernate will lads this object from the database as this is the first time in the session
- At line number 4, i printed my data bla bla..
- then in line number 6, i removed this object [ with id 101 ] from the cache memory of the session by calling evict() method
- Now in line number 8 again i tried to load the same object, so as we are in the same session hibernate first will verify whether the object is there in the cache or not, if not loads the object from the database, but we removed the object from the cache with evict() method right, so hibernate will loads from the database
- I mean, first checks at local session then only from the database if its not available in the local cache
And that’s it mates ? …………!!!!!!!!
post a comment