To enable second level cache, here we will use EHCache.
Change mapping file category.hbm.xml, and one line
1 2 3 |
<cache usage="read-write"/> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="net.tecbar.domain.Category" table="category"> <cache usage="read-write"/> <id name="id" type="integer"> <column name="id" /> <generator class="native"></generator> </id> <property name="name" type="string"> <column name="name" /> </property> <property name="description" type="string"> <column name="description" /> </property> </class> </hibernate-mapping> |
add one ehcache.xml, a simple example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<ehcache> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> </ehcache> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="net.tecbar.domain.Category" table="category"> <cache usage="read-write"/> <id name="id" type="integer"> <column name="id" /> <generator class="native"></generator> </id> <property name="name" type="string"> <column name="name" /> </property> <property name="description" type="string"> <column name="description" /> </property> </class> </hibernate-mapping> |
Let’s use the same code as we used in the 1st level cache example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package net.tecbar.hibernate; import net.tecbar.domain.Category; public class HibernateSecondLevelCacheExample { // we enabled 2nd level cache public static void main(String[] args) throws InterruptedException { // query database and put the entity into the 1st level cache and second level cache final Category category = (Category)HibernateUtil.getSession().get(Category.class, 3); System.out.println(category); HibernateUtil.closeSession(); // The entity was removed after the previous session close. // However, it is still available in second level cache // it will search the second level cache. // Once the record is found, it will be put it into the 1st level cache again final Category category2 = (Category)HibernateUtil.getSession().get(Category.class, 3); System.out.println(category2); HibernateUtil.closeSession(); } } |
The output:
1 2 3 4 5 |
Hibernate: select category0_.id as id1_0_0_, category0_.name as name2_0_0_, category0_.description as descript3_0_0_ from category category0_ where category0_.id=? PIAccount [id=3, name=car, description=2015 models] PIAccount [id=3, name=car, description=2015 models] |
Part of Hibernate Debug level log
1 2 3 4 5 6 7 8 9 10 11 12 13 |
DEBUG [SQL]:109 - select category0_.id as id1_0_0_, category0_.name as name2_0_0_, category0_.description as descript3_0_0_ from category category0_ where category0_.id=? DEBUG [LogicalConnectionImpl]:226 - Obtaining JDBC connection DEBUG [LogicalConnectionImpl]:232 - Obtained JDBC connection DEBUG [ResultSetProcessorImpl]:127 - Starting ResultSet row #0 DEBUG [EntityReferenceInitializerImpl]:142 - On call to EntityIdentifierReaderImpl#resolve, EntityKey was already known; should only happen on root returns with an optional identifier specified DEBUG [TwoPhaseLoad]:160 - Resolving associations for [net.tecbar.domain.Category#3] DEBUG [TwoPhaseLoad]:194 - Adding entity to second-level cache: [net.tecbar.domain.Category#3] DEBUG [TwoPhaseLoad]:286 - Done materializing entity [net.tecbar.domain.Category#3] DEBUG [AbstractLoadPlanBasedEntityLoader]:208 - Done entity load : net.tecbar.domain.Category#3 [LogicalConnectionImpl]:246 - Releasing JDBC connection DEBUG [LogicalConnectionImpl]:264 - Released JDBC connection |
Pingback: Hibernate Caching