Cache is representation of database near to application. when you cache the read-mostly(which will be repeatedly used) data, you can reduce the number of round trip between your application server and database server. Which increase performance of your application.
In general you can have three types of cache scope :
In general you can have three types of cache scope :
- Transactional scope cache.
- Process scope cache.
- Clustered scope cache.
2. Process scope cache : This kind of cache can be accessed by multiple transaction running in the application or process.
3. Clustered scope cache : When your enterprise application is large scale application then you must use clustered environment where multiple server is clustered and integrated with LBS(load balancing server) for running the request. In clustered scope cache when one node caches the data then same data will also be replicated to other nodes automatically.
First Level Cache :
First Level Cache :
- Session cache is first level cache.
- Session cache is transnational cache.
- Session cache is enabled by default and should not be disabled.
- When ever you insert or update or select or delete the records then those persistence object will be placed automatically in session cache.
- You can remove the persistence object from session cache using evict(obj) and clear().
- Query cache is second level cache.
- Query cache can be process scope and cluster scope cache.
- Query cache is disabled by default. You have to enable explicitly if required.
- When ever you execute the hibernate Queries HQL, QBC etc then all records returned by select statement will be placed in Query cache.
- Enable the query cache by writing the following property in hibernate configuration doc.
<property name="hibernate.cache.use_query_cache">true</property> - Specify the required cache provider in hibernate configuration document.
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
Note : Cache provider supported by hibernate
->HashtableCacheProvider
->EhCacheProvider
->OSCacheProvider
->SwarmCacheProvider
->TreeCacheProvider(jboss cache provider) - Specify the required caching concurrency strategies in hibernate mapping doc.
<cache usage="xx">
here xx can be one of the following
->read-only
->nonstrict-read-write
->read-write
->transactional
Note : You have to specify caching concurrency strategies separately for class, collections and Associations. - call setCacheable() method with true value on query or criteria or SQLQuery objects.
Query q=session.createQuery("from customer");
q.setCachable(true);
List list=q.list(); - Specify the storage implementations and Expiration policies
Note : this will change from cache provider to provider.
ReplyDeletenice article for beginners.thank you.
java tutorial
java tutorial