What is Caching ?
Caching is widely used for optimizing database applications.
Hibernate uses two different caches for objects :
- First-level cache : associated with the Session object (used by default)
- Second-level cache : associated with the Session Factory 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.
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.
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 Types
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
Caching strategies
Read-only
This strategy is useful for data that is read frequently but never updated. This is by far the simplest and best-performing cache strategy.
Read / write
Read/write caches may be appropriate if your data needs to be updated.
They carry more overhead than read-only caches.
In non-JTA environments, each transaction should be completed when Session.close() or Session.disconnect() is called.
Nonstrict read / write
This strategy does not guarantee that two transactions won't simultaneously modify the same data.
Therefore, it may be most appropriate for data that is read often but only occasionally modified.
Transactional
This is a fully transactional cache that may be used only in a JTA environment.
Configure 2nd level cache
To activate second-level caching, you need to define the hibernate.cache.provider_class property in the hibernate.cfg.xml file as follows :
<hibernate-configuration>
<session-factory>
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EHCacheProvider
</property>
</session-factory>
</hibernate-configuration>
By default, the second-level cache is activated and uses the EHCache provider.
To use the query cache you must first enable it by setting the property hibernate.cache.use_query_cache to true in hibernate.properties.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.