Using JBoss Cache as a Hibernate Second Level Cache
=============================================================
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
">
<bean id="cisSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" scope="singleton">
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.jbc2.JndiMultiplexedJBossCacheRegionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.jbc2.cachefactory">java:CacheManager</prop>
<prop key="hibernate.cache.region.jbc2.cfg.entity">mvcc-entity</prop>
<prop key="hibernate.cache.region.jbc2.cfg.query">local-query</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.test.persistence.entity</value>
</list>
</property>
<property name="dataSource" ref="persistence.datasource"/>
<property name="jtaTransactionManager" ref="jtaTransactionManager"/>
</bean>
<bean id="persistence.datasource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:TestDS</value>
</property>
</bean>
<bean id="testDao" class="com.test.dao.hibernate.TestDAOImpl">
<property name="sessionFactory" ref="testSessionFactory"/>
</bean>
<bean id="jtaTransactionManager" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>java:/TransactionManager</value></property>
</bean>
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
<aop:aspectj-autoproxy/>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
========================================================
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class TestDAOImpl extends HibernateDaoSupport {
public List<StudentEntity> getStudents() {
getHibernateTemplate().setQueryCacheRegion("/com/test/Student");
getHibernateTemplate().setCacheQueries(true);
List<StudentEntity> studentList = getHibernateTemplate()
.findByNamedQuery("student.All");
return studentList;
}
}
======================================================
1.The JndiMultiplexedJBossCacheRegionFactory requires that the JBoss Cache CacheManager
is already bound in JNDI; it will not create and bind one if it isn't. It is up to the the user to ensure
the cache is CacheManager and bound in JNDI before the Hibernate SessionFactory is created.
2.For non-JPA Hibernate second level caching, the same configuration properties and values are used;
they are just declared in a Hibernate SessionFactory cfg.xml
file using its syntax (and with "hibernate." removed from the property names):
1.The JndiMultiplexedJBossCacheRegionFactory requires that the JBoss Cache CacheManager
is already bound in JNDI; it will not create and bind one if it isn't. It is up to the the user to ensure
the cache is CacheManager and bound in JNDI before the Hibernate SessionFactory is created.
2.For non-JPA Hibernate second level caching, the same configuration properties and values are used;
they are just declared in a Hibernate SessionFactory cfg.xml
file using its syntax (and with "hibernate." removed from the property names):
3. JBoss Cache requires integration with a JTA TransactionManager in order to meet the
requirements of the second level caching use case. This means your Hibernate application must
be configured to use JTA:
Single JBoss Cache Instance Architecture
For the single cache case, the user should specify SharedJBossCacheRegionFactory as their
hibernate.cache.region.factory_class
Multiple JBoss Cache Instance Architecture
Here the user should specify MultiplexedJBossCacheRegionFactory as their
hibernate.cache.region.factory_class. The MultiplexedJBossCacheRegionFactory
shares almost all its code with SharedJBossCacheRegionFactory; the main
difference is it constructs a different CacheInstanceManager implementation -- the
MultiplexedCacheInstanceManager.
Hibernate Second Level Caching in JBoss AS 5
Clustered JPA
http://opensourceframework.blogspot.com/2010/08/caching-queries-with-hibernatetemplate.html
----
---
ANish
Good one but HIberanate level 2 cache does not work properly with JtaTransactionmanger.
ReplyDeleteNice explanation but I’d rather prefer NCache because its second level cache works better with NHibernate. NCache provides NHibernate integration for Second Level Cache without any code change. Thereby it is easy to configure. Configuration Details
ReplyDelete