4

Where is first level cache stored in Hibernate? In memory(RAM) or hard disk? incase it is stored in memory, if the memory is less for storing all the rows of the query, how does it manage the cache in this case?

Jyotirup
  • 2,882
  • 9
  • 30
  • 38

4 Answers4

3

Hibernate Session is it's first level cache. It is object in heap, so it is in RAM. Usually you have enough RAM (>256MB) to store queries =)

Pavel Vyazankin
  • 1,470
  • 6
  • 18
  • 27
1

its in memory, otherwise it wouldn't offer any performance advantage.

ehcache memcached hazelcast

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • so if the cache is full, will hibernate execute the query again to get all the rows or optmise the query to get the missing rows? – Jyotirup Mar 05 '13 at 09:00
  • if the cache is full and there is no memory availbale, you can't add anythign to it - I imagie different cahce roviders behave differently. – NimChimpsky Mar 05 '13 at 09:01
  • What is your strange definition of "a cache", that excludes a cache being on disk? Suppose you have query that takes a loooooong time to execute, and the results are somewhat huge. Couldn't it be interesting, if the requirements allow, to *cache* the results by saving them to a file on disk? You have a disk cache improving the performance of a database query. – Bruno Reis Mar 05 '13 at 09:02
  • @BrunoReis every single cache implementation related to java/hibernate uses RAM to cache the results from disk. And in your example, I don't think that would improve performance in 99.9% of uses case. – NimChimpsky Mar 05 '13 at 09:06
  • @BrunoReis I'm talking about first level cache or session level cache which is default in hibernate – Jyotirup Mar 05 '13 at 09:06
0

Generally Caches are stored in Memory but you can access caches from the network if you use clustered caches.

Here a list of Hibernate cache providers and difference between them : Hibernate cache providers

You can find some other cache providers that may use NoSql key/value engines to handle caching.

Houcem Berrayana
  • 3,052
  • 22
  • 40
  • you are talking about second level cache. I'm talking about first level cache or session level cache which is default in hibernate. – Jyotirup Mar 05 '13 at 09:05
  • You are right. Sorry for the inattention. The first level cache is a stored in Session, which means in memory. Generally when it's not properly used you can have a Memory errors because Hibernate does not limit that memory by default – Houcem Berrayana Mar 05 '13 at 09:09
0

Frist-level cache is definitely in RAM which session holds, so it has performance and easy-to-use advantage.

If you think the loaded objects is too many in session, you can evict some from first-level cache before session is closed. see Session interface source code.

public interface Session extends Serializable {
    ...

    /**
     * Remove this instance from the session cache. Changes to the instance will
     * not be synchronized with the database. This operation cascades to associated
     * instances if the association is mapped with <tt>cascade="evict"</tt>.
     *
     * @param object a persistent instance
     * @throws HibernateException
     */
    public void evict(Object object) throws HibernateException;
}
Henry Leu
  • 2,184
  • 4
  • 22
  • 34