google Analytics

Monday, August 30, 2010

Caching queries with HibernateTemplate

hibernateTemplate 2nd level cache
Caching queries with HibernateTemplate
hibernateTemplate caching
Speed Up Your Hibernate Applications with Second-Level Caching
hibernate jboss cache guide

Hibernate Caching with JBOSS Treecache example
Hibernate Caching
=============================================================
 

=============================================================

The treecache.xml
<?xml version="1.0" encoding="UTF-8"?>
<server>
    <mbean code="org.jboss.cache.TreeCache" name="jboss.cache:service=TreeCache">
        <depends>jboss:service=TransactionManager</depends>
        <!-- Configure the TransactionManager -->
        <attribute name="TransactionManagerLookupClass">org.jboss.cache.JBossTransactionManagerLookup</attribute>
        <!-- Isolation level : SERIALIZABLE
                               REPEATABLE_READ (default)
                               READ_COMMITTED
                               READ_UNCOMMITTED
                               NONE
        -->
        <attribute name="IsolationLevel">REPEATABLE_READ</attribute>

        <!-- Valid modes are LOCAL, REPL_ASYNC and REPL_SYNC -->
        <attribute name="CacheMode">LOCAL</attribute>
       
        <!-- Just used for async repl: use a replication queue -->
        <attribute name="UseReplQueue">false</attribute>

        <!-- Replication interval for replication queue (in ms) -->
        <attribute name="ReplQueueInterval">0</attribute>

        <!-- Max number of elements which trigger replication -->
        <attribute name="ReplQueueMaxElements">0</attribute>

        <!-- Whether or not to fetch state on joining a cluster -->
        <attribute name="FetchStateOnStartup">true</attribute>

        <!-- The max amount of time (in milliseconds) we wait until the
             initial state (ie. the contents of the cache) are retrieved from
             existing members in a clustered environment
        -->
        <attribute name="InitialStateRetrievalTimeout">5000</attribute>

        <!-- Number of milliseconds to wait until all responses for a synchronous call have been received. -->
        <attribute name="SyncReplTimeout">15000</attribute>

        <!-- Max number of milliseconds to wait for a lock acquisition -->
        <attribute name="LockAcquisitionTimeout">15000</attribute>

        <!-- Name of the eviction policy class. -->
        <!--
        <attribute name="EvictionPolicyClass">org.jboss.cache.eviction.FIFOPolicy</attribute>
        -->
        <!--  Specific eviction policy configurations. This is LRU -->

        <attribute name="EvictionPolicyConfig">
          <config>
            <attribute name="wakeUpIntervalSeconds">5</attribute>
            <region name="/_default_">
             <attribute name="maxNodes">5000</attribute>
             <attribute name="timeToLiveSeconds">1000</attribute>
             <attribute name="maxAgeSeconds">120</attribute>
            </region>
            <region name="hibernate/HibernateLogEntryDao">
             <attribute name="maxNodes">5000</attribute>
             <attribute name="timeToLiveSeconds">1000</attribute>
            </region>
          </config>
        </attribute>

    </mbean>
</server>





=================================================
The treecache.xml,to be placed in the common location where log4j properties files are read


Jar's Needed Jboss-cache.jar and jgroups.jar and comman hibernate Jar's


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:jboss="http://www.springmodules.org/schema/jboss"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                           http://www.springmodules.org/schema/jboss
                           http://www.springmodules.org/schema/cache/springmodules-jboss.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
        <property name="url"><value>jdbc:oracle:thin:@localhost:1521:XE</value></property>
        <property name="username"><value>smc</value></property>
        <property name="password"><value>smc</value></property>
        <property name="maxActive"><value>100</value></property>
        <property name="maxIdle"><value>30</value></property>
        <property name="maxWait"><value>1000</value></property>
        <property name="defaultAutoCommit"><value>false</value></property>
    </bean>

    <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingLocations">
            <list>
                <value>classpath*:/dataaccess/hibernate/*.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.TreeCacheProvider</prop>

                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
            </props>
        </property>
    </bean>

    <bean id="hibernateDaoSupport" abstract="true" class="org.springframework.orm.hibernate3.support.HibernateDaoSupport">
        <property name="sessionFactory" ref="hibernateSessionFactory"/>
    </bean>
 

    <bean id="templatelessLogEntryDao" class="com.apress.prospring2.ch11.dataaccess.hibernate.TemplatelessHibernateInvoiceLogEntryDao">
        <constructor-arg ref="hibernateSessionFactory"/>
    </bean>
 

  <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
     <bean id="lodDao" class="com.apress.prospring2.ch11.dataaccess.HiberNateDaoSupportIml">
         <property name="sessionFactory" ref="hibernateSessionFactory"/>
     </bean>
</beans>





Implementation Code:-

  public List<LogEntry> getAll() {
        System.out.println("GetAll Called");
        long start, end, total;
        start = System.currentTimeMillis();
        getHibernateTemplate().setQueryCacheRegion("HibernateLogEntryDao");
        getHibernateTemplate().setCacheQueries(true);

        List<LogEntry> returns=getHibernateTemplate().find("from LogEntry");
        end = System.currentTimeMillis();
        total = end - start;
        System.out.println("Totall-->>"+total);
        return returns;
       
    }





hibernateTemplate 2nd level cache

Caching queries with HibernateTemplate

hibernateTemplate caching

Speed Up Your Hibernate Applications with Second-Level Caching

hibernate jboss cache guide
Using treecache as 2nd level cache
JBoss Cache based Hibernate Second Level Cache provider

2 comments:

  1. Once I set caching for one query in one method of my DAO using "getHibernateTemplate().setCacheQueries(true);", then it is caching queries from all methods. Is there any solution to cache only queries for selected method

    ReplyDelete