Caching is widely used for optimizing database applications. Hibernate uses two different caches for objects :
First-level cache
First-level cache is associated with the Session object by default.
Hibernate uses first-level cache on a per-transaction basis.
Hibernate uses this cache mainly to reduce the number of SQL queries it needs to generate within a given transaction.
For
example, if an object is modified several times within the same
transaction, Hibernate will generate only one SQL UPDATE statement at
the end of the transaction, containing all the modifications.
Second-level cache
Second-level cache is associated with the Session Factory object.
To reduce database traffic, second-level cache keeps loaded objects at the Session Factory level between transactions.
These
objects are available to the whole application, not just to the user
running the query. This way, each time a query returns an object that is
already loaded in the cache, one or more database transactions
potentially are avoided.
Query-level cache
In addition, you can use a query-level cache if you need to cache actual query results, rather than just persistent objects. The query cache should always be used in conjunction with the second-level cache.
Cache implementations
Hibernate supports the following open-source Cache implementations out-of-the-box :
1. EHCache
- fast, lightweight, and easy-to-use in-process cache.
- supports read-only and read/write caching, and memory- and disk-based caching.
- does not support clustering.
2. OSCache
- Open-source caching solution
Part of a larger package, which also provides caching functionality for JSP pages or arbitrary objects. - Like EHCache, supports read-only and read/write caching, and memory- and disk-based caching.
- Also provides basic support for clustering via either JavaGroups or JMS.
3. SwarmCache
- Simple cluster-based caching solution based on JavaGroups.
- supports read-only or nonstrict read/write caching
- Appropriate for applications that typically have many more read operations than write operations.
4. JBoss / TreeCache
- A powerful replicated (synchronous or asynchronous) and transactional cache.
- Used for true transaction-capable caching architecture
No comments:
Post a Comment
Note: only a member of this blog may post a comment.