Can anyone explain in simple words what First and Second Level caching in Hibernate are?
相关问题
- How does the JPA handle partial, non-disjoint inhe
- Tell hibernate hbm2ddl not create individual table
- Should there be an EntityManager per thread in Spr
- Hibernate and multiThread Logic
- Why does Hibernate perform a JNDI lookup?
相关文章
- Hibernate Tutorial - Where to put Mapping File?
- Hibernate doesn't generate cascade
- Fluent NHibernate — Saving Entity with Composite K
- Setup and Tear Down of Complex Database State With
- Is this the right way of using ThenFetch() to load
- Can Persistence Ignorance Scale?
- How to map an abstract collection with jpa?
- Two foreign keys as primary key
There's a pretty good explanation of first level caching on the Streamline Logic blog.
Basically, first level caching happens on a per session basis where as second level caching can be shared across multiple sessions.
In a second level cache, domain hbm files can be of key mutable and value false. For example, In this domain class some of the duration in a day remains constant as the universal truth. So, it can be marked as immutable across application.
First-level cache
Hibernate tries to defer the Persistence Context flushing up until the last possible moment. As I explained in this article, this strategy has been traditionally known as transactional write-behind.
The write-behind is more related to Hibernate flushing rather than any logical or physical transaction. During a transaction, the flush may occur multiple times.
The flushed changes are visible only for the current database transaction. Until the current transaction is committed, no change is visible by other concurrent transactions.
Due to the first-level cache, Hibernate can do several optimizations:
Second-level cache
A proper caching solution would have to span across multiple Hibernate Sessions and that’s the reason Hibernate supports an additional second-level cache as well.
The second-level cache is bound to the SessionFactory life-cycle, so it’s destroyed only when the
SessionFactory
is closed (typically when the application is shutting down). The second-level cache is primarily entity-based oriented, although it supports an optional query-caching solution as well.For more details, check out this article.
Here some basic explanation of hibernate cache...
First level cache is associated with “session” object. The scope of cache objects is of session. Once session is closed, cached objects are gone forever. First level cache is enabled by default and you can not disable it. When we query an entity first time, it is retrieved from database and stored in first level cache associated with hibernate session. If we query same object again with same session object, it will be loaded from cache and no sql query will be executed. The loaded entity can be removed from session using
evict()
method. The next loading of this entity will again make a database call if it has been removed usingevict()
method. The whole session cache can be removed usingclear()
method. It will remove all the entities stored in cache.Second level cache is apart from first level cache which is available to be used globally in session factory scope. second level cache is created in session factory scope and is available to be used in all sessions which are created using that particular session factory. It also means that once session factory is closed, all cache associated with it die and cache manager also closed down. Whenever hibernate session try to load an entity, the very first place it look for cached copy of entity in first level cache (associated with particular hibernate session). If cached copy of entity is present in first level cache, it is returned as result of load method. If there is no cached entity in first level cache, then second level cache is looked up for cached entity. If second level cache has cached entity, it is returned as result of load method. But, before returning the entity, it is stored in first level cache also so that next invocation to load method for entity will return the entity from first level cache itself, and there will not be need to go to second level cache again. If entity is not found in first level cache and second level cache also, then database query is executed and entity is stored in both cache levels, before returning as response of
load()
method.First Level Cache
Session object holds the first level cache data. It is enabled by default. The first level cache data will not be available to entire application. An application can use many session object.
Second Level Cache
SessionFactory object holds the second level cache data. The data stored in the second level cache will be available to entire application. But we need to enable it explicitly.
1.1) First-level cache
1.2) Second-level cache
Quoted from: http://javabeat.net/introduction-to-hibernate-caching/