Search code examples
hibernatesessionfactory

Second Level Cache isn't active in Hibernate?


i'm studing Second Hibernate Cache in Hibernate. i have a problem, i spend more time to explore , but not.

  • I use Oracle, and create table by annotation.

hibernate.cfg.xml:

<session-factory>
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:quan</property>
    <property name="connection.username">system</property>
    <property name="connection.password">123456</property>

    <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

     <!-- Enable Second Level Cache -->
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="net.sf.ehcache.configurationResourceName">ehcache.xml</property>
    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>


    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    <mapping class="com.javamakeuse.poc.pojo.Employee"/>

</session-factory>

Main class:

Transaction t=null;

        t=s.beginTransaction();
        Employee e=(Employee)s.get(Employee.class, new Integer(1));
        if(e!=null){
        System.out.print("ID :"+e.getEmpid());
        System.out.print(", Name :"+e.getEmpname());
        System.out.print(", Phone :"+e.getEmpmobile());
        System.out.println("Salary :"+e.getEmpsalary());
        }else{
            System.out.println("No records:!!");
        }

        //// second time loading same entity from the first level cache

        e=(Employee)s.get(Employee.class, new Integer(1));
        if(e!=null){
        System.out.print("ID :"+e.getEmpid());
        System.out.print(", Name :"+e.getEmpname());
        System.out.print(", Phone :"+e.getEmpmobile());
        System.out.println("Salary :"+e.getEmpsalary());
        }else{
            System.out.println("No records:!!");
        }

        // remove Cache
        s.evict(e);
        // Going to print Employee*** from Second level Cache
        // Why i have a new query???????????????????

         e=(Employee)s.get(Employee.class, new Integer(1));
        if(e!=null){
        System.out.print("ID :"+e.getEmpid());
        System.out.print(", Name :"+e.getEmpname());
        System.out.print(", Phone :"+e.getEmpmobile());
        System.out.println("Salary :"+e.getEmpsalary());
        }else{
            System.out.println("No records:!!");
        }

        t.commit();

ehcache.xml

<ehcache>
<defaultCache maxEntriesLocalHeap="4000" eternal="false"
    timeToIdleSeconds="60" timeToLiveSeconds="120" diskSpoolBufferSizeMB="20"
    maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
    memoryStoreEvictionPolicy="LRU" statistics="true">
    <persistence strategy="localTempSwap" />
</defaultCache>

<cache name="country" maxEntriesLocalHeap="4000" eternal="false"
    timeToIdleSeconds="5" timeToLiveSeconds="10">
    <persistence strategy="localTempSwap" />
</cache>

<cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
    maxEntriesLocalHeap="5000" eternal="true">
    <persistence strategy="localTempSwap" />
</cache>

Console Output

ID :1Hibernate: 
select
    employee0_.empid as empid1_0_0_,
    employee0_.empmobile as empmobile2_0_0_,
    employee0_.empname as empname3_0_0_,
    employee0_.empsalary as empsalary4_0_0_ 
from
    employee2 employee0_ 
where
    employee0_.empid=?
Name :Nguyen VQ, Phone :948380166Salary :6.2
ID :1, Name :Nguyen VQ, Phone :948380166Salary :6.2
ID :1Hibernate: 
select
    employee0_.empid as empid1_0_0_,
    employee0_.empmobile as empmobile2_0_0_,
    employee0_.empname as empname3_0_0_,
    employee0_.empsalary as empsalary4_0_0_ 
from
    employee2 employee0_ 
where
    employee0_.empid=?
Name :Nguyen VQ, Phone :948380166Salary :6.2
ID4: 1,Name4 :Nguyen VQ,Mobile: 948380166, Salary: 6.2

When i remove e by s.evict(e); then it comes to SessionFactory, but it is not working (it create new query)


Solution

  • Second level configuration seems to be set up properly. One thing I suspect is @Cache attribute. Did you enable CacheConcurrencyStrategy on Employee object using @Cache annotation.

    @Cache(usage=CacheConcurrencyStrategy.READ_WRITE | NONSTRICT_READ_WRITE | TRANSACTIONAL)