Archive for the ‘Spring’

Caching in Spring Hibernate application

March 09, 2012 By: admin Category: Hibernate, Spring

  • Object or entity caching
  • Method’s return value caching

Object or entity caching

  • Write a custom cache provider class
  • package com.technaugh.cache;
    
    import java.util.Properties;
    import org.hibernate.cache.Cache;
    import org.hibernate.cache.CacheException;
    import org.hibernate.cache.CacheProvider;
    import net.sf.ehcache.CacheManager;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.cache.Timestamper;
    
    /**
     *
     *   Class      : CustomCacheProvider
     *   Created on : Jan 12, 2011 4:27:24 PM
     *   Author     : arahman
     */
    public class CustomCacheProvider implements CacheProvider {
    
        private static Log logger = LogFactory.getLog(CacheProvider.class);
        private CacheManager cacheManager;
    
        public CustomCacheProvider(CacheManager cacheManager) {
            this.cacheManager = cacheManager;
        }
    
        public Cache buildCache(String name, Properties props) throws CacheException {
            try {
                net.sf.ehcache.Ehcache cache = cacheManager.getEhcache(name);
                if (cache == null) {
                    cacheManager.addCache(name);
                    cache = cacheManager.getEhcache(name);
                }
                logger.debug("Cache details: "+cache.toString());
    
                return new net.sf.ehcache.hibernate.EhCache(cache);
            } catch (net.sf.ehcache.CacheException e) {
                throw new CacheException(e);
            }
        }
    
        public long nextTimestamp() {
            return Timestamper.next();
        }
    
        public void start(Properties arg0) throws CacheException {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void stop() {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public boolean isMinimalPutsEnabledByDefault() {
            return false;
        }
    }
    
  • Define Spring bean ‘sessionFactory’
  • 
    
    
    
    
    
        
             
        
      
        
                   
        
     
        
    
            
                
            
     
              
                
            
           
            
                
                    org.hibernate.dialect.MySQLDialect
                    true
                    true 
                    
                     true
                     true
                
            
        
           
           
    			
    				classpath:/bean/hibernate
    			
    		
        
    
    
  • Create ehcache-wwl.xml
  • 
    
    
     
     
     
    
      
      
      
      
    
    
    
  • Configure object mapping ( in *hbm.xml file) files for caching
  • 
    
    
    
    
        
            
          
            
                
                
            
     
         
     
        
    
    
    
Subscribe To Site: