How to configure the Hibernate second level cache using Hibernate
We are trying to configure the Hibernate second level cache as Ehcache using spring framework .
While configuring it I got following exception. Changes that I have done for this are mentioned below
org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'transactionManager' defined in class path resource [com/manh/ils/db/test/test-spring-config.xml]: Can't resolve reference to bean 'sessionFactory' while setting property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sessionFactory' defined in class path resource [com/manh/ils/db/test/test-spring-config.xml]: Initialization of bean failed; nested exception is java.lang.AbstractMethodError: javax.xml.parsers.SAXParser.getXMLReader()Lorg/xml/sax/XMLReader;
org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sessionFactory' defined in class path resource [com/manh/ils/db/test/test-spring-config.xml]: Initialization of bean failed; nested exception is java.lang.AbstractMethodError: javax.xml.parsers.SAXParser.getXMLReader()Lorg/xml/sax/XMLReader;
java.lang.AbstractMethodError: javax.xml.parsers.SAXParser.getXMLReader()Lorg/xml/sax/XMLReader;
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at net.sf.ehcache.config.ConfigurationFactory.parseCo nfiguration(ConfigurationFactory.java:145)
at net.sf.ehcache.config.ConfigurationFactory.parseCo nfiguration(ConfigurationFactory.java:93)
at net.sf.ehcache.config.ConfigurationFactory.parseCo nfiguration(ConfigurationFactory.java:131)
at net.sf.ehcache.CacheManager.parseConfiguration(Cac heManager.java:255)
at net.sf.ehcache.CacheManager.init(CacheManager.java :202)
at net.sf.ehcache.CacheManager.<init>(CacheManager.ja va:195)
at net.sf.ehcache.CacheManager.create(CacheManager.ja va:364)
at org.hibernate.cache.EhCacheProvider.start(EhCacheP rovider.java:124)
at org.hibernate.impl.SessionFactoryImpl.<init>(Sessi onFactoryImpl.java:169)
at org.hibernate.cfg.Configuration.buildSessionFactor y(Configuration.java:1005)
at org.springframework.orm.hibernate3.LocalSessionFac toryBean.newSessionFactory(LocalSessionFactoryBean .java:800)
at org.springframework.orm.hibernate3.LocalSessionFac toryBean.afterPropertiesSet(LocalSessionFactoryBea n.java:726)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:1059)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:363)
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:226)
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:147)
at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:176)
at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:105)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1013)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:824)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:345)
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:226)
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:147)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:275)
at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:320)
at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:87)
at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:72)
at org.springframework.test.AbstractSpringContextTest s.loadContextLocations(AbstractSpringContextTests. java:121)
at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.loadContextLocations(Abstract DependencyInjectionSpringContextTests.java:210)
at org.springframework.test.AbstractSpringContextTest s.getContext(AbstractSpringContextTests.java:101)
at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.setUp(AbstractDependencyInjec tionSpringContextTests.java:178)
at junit.framework.TestCase.runBare(TestCase.java:125 )
at junit.framework.TestResult$1.protect(TestResult.ja va:106)
at junit.framework.TestResult.runProtected(TestResult .java:124)
at junit.framework.TestResult.run(TestResult.java:109 )
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)
These are the following changes we are done:
test-spring-config.xml:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean"
depends-on="connectionProviderRegistrar">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<list>
<value>com/manh/ils/db/test/Item.hbm.xml</value>
<value>com/manh/ils/db/test/User.hbm.xml</value>
<value>com/manh/ils/db/test/Auction.hbm.xml</value>
<value>com/manh/ils/db/test/Bid.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${test.db.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.provider_class">com.logi stics.javalib.persistence.layer.JDBCConnectionProv ider</prop>
<prop key="hibernate.cache.provider_configuration_file_r esource_path">com/manh/ils/db/test/ehcache.xml</prop>
<prop key="hibernate.cache.provider_class">org.hibernate .cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
</props>
</property>
</bean>
ehcahe.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"/>
<ehcache>
<diskStore path="java.io.tmp"/>
<defaultCache
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"/>
<cache name="com.manh.ils.db.test.Item"
maxElementsInMemory="20"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="180"
overflowToDisk="true"/>
</ehcache>
Item.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.manh.ils.db.test">
<class name="Item">
<cache usage="read-write"/>
<id name="id" column="ITEM_ID">
<generator class="native" />
</id>
<property name="name" type="string" />
<property name="description" type="string" />
</class>
<query name="Item.testQuery">from Item</query>
<query name="Item.testUpdate">update Item set description='vague description'</query>
</hibernate-mapping>