www.JohnJustin.tk
TECH A BREAK 2010 PHOTOS
friendsclub academic project
librarymanagementsystem
AirwayReservationsystem
Payroll Calculation Project
Online Banking project
DOWNLOAD 125 JAVA PROJECTS FREE
FREE DOWNLOAD KAROAKE
FOR ADVERTISE HERE
CONTACT 917667566149

Sunday, May 16, 2010

what is caching in hibernate ? example with sample code simple hibernate caching

In Hibernate web applications we will face so many problems in its performance due to database traffic. That to when the database traffic is very heavy . Actually hibernate is well used just because of its high performance only.  Caching is the best technique to solve this problem. 
 One of the primary concerns of mappings between a database and your Java application is performance. One of the common concerns of people who haven't spent much time working with Hibernate in particular, is that O/R mapping tools will limit your ability to make performance-enhancing changes to particular queries and retrievals

The cache actually stores the data already loaded from the database, so that the traffic between our application and the database will be reduced when the application want to access that data again. Maximum the application will works with the data in the cache only. Whenever some another data is needed, the database will be accessed. Because the time needed to access the database is more when compared with the time needed to access the cache. So obviously the access time and traffic will be reduced between the application and the database. Here the cache stores only the data related to current running application. In order to do that, the cache must be cleared time to time whenever the applications are changing. Here are the contents.
Hibernate uses two different caches for objects: first-level cache and second-level cache

First-level cache always Associates with the Session object. Hibernate uses this cache by default Hibernate actually implements a simple session-level cache, useful on a per-transaction basis. This cache is primarily used to optimize the SQL generated by Hibernate. It is sometimes referred to as a first-level Hibernate cache

The Second Level Cache
The second-level cache is called 'second-level' because there is already a cache operating for you in Hibernate for the duration you have a session open. From the Hibernate documentation:
A Hibernate Session is a transaction-level cache of persistent data. It is possible to configure a cluster or JVM-level (SessionFactory-level) cache on a class-by-class and collection-by-collection basis. You may even plug in a clustered cache. Be careful. Caches are never aware of changes made to the persistent store by another application (though they may be configured to regularly expire cached data).
As implied above, this 'second-level' cache exists as long as the session factory is alive. The second-level cache holds on to the 'data' for all properties and associations (and collections if requested) for individual entities that are marked to be cached. 

 HOW TO IMPLIMENT HIBERNATE CACHES

Hibernate supports four open-source cache implementations named EHCache (Easy Hibernate Cache), OSCache (Open Symphony Cache), Swarm Cache, and JBoss Tree Cache. Each cache has different performance, memory use, and configuration possibilities.

EHCache (Easy Hibernate Cache) (org.hibernate.cache.EhCacheProvider)

http://ehcache.sourceforge.net/

OSCache (Open Symphony Cache) (org.hibernate.cache.OSCacheProvider) 

http://www.opensymphony.com/oscache/

SwarmCache (org.hibernate.cache.SwarmCacheProvider) 

http://swarmcache.sourceforge.net/

JBoss TreeCache (org.hibernate.cache.TreeCacheProvider) 

http://jboss.org/wiki/Wiki.jsp?page=JBossCache

WHAT ARE THE CACHING STRATEGIES IN HIBERNATE? 

Read-only

....
 

Read-Write

When Session.close() or Session.disconnect() is called the transaction should be completed in an environment where JTA is no used


  • In a JTA environment, for obtaining the JTA TransactionManager we must specify the property hibernate.transaction.manager_lookup_class.

  • To use it in a cluster the cache implementation must support locking.

  •  

    example for using the read-write cache strategy

    ….….
     

    Nonstrict read-write


  • we must specify hibernate.transaction.manager_lookup_class to use this in a JTA environment .

  • The transaction is completed when Session.close() or Session.disconnect() is called In other environments (except JTA) .


  • example for using the nonstrict read-write cache stringategy.

    ….

     
     

     

     

     

     


    0 comments:

    JohnJustin
    software engineer,blogger,writer,prolife worker and an active JesusYouth
    johnjustin@in.com
    TECH A BREAK 2010 PHOTOS
    friendsclub academic project
    librarymanagementsystem
    AirwayReservationsystem
    Payroll Calculation Project
    Online Banking project
    DOWNLOAD 125 JAVA PROJECTS FREE
    FREE DOWNLOAD KAROAKE
    FOR ADVERTISE HERE
    CONTACT 917667566149

    .
    .
    .

    Subscribe feeds via e-mail
    Subscribe in your preferred RSS reader

    Subscribe feeds rss Recent Entries

    Advertise on this site Sponsored links

    Categories

    Sponsored Links

    .
    TECH A BREAK 2010 PHOTOS
    friendsclub academic project
    librarymanagementsystem
    AirwayReservationsystem
    Payroll Calculation Project
    Online Banking project
    DOWNLOAD 125 JAVA PROJECTS FREE
    FREE DOWNLOAD KAROAKE
    FOR ADVERTISE HERE
    CONTACT 917667566149

    .

    My Photos on flickr

    Subscribe feeds rss Recent Comments

    Technorati

    Technorati
    My authority on technorati
    Add this blog to your faves