java - Spring migration 4.1.X to 4.2.x 2nd level caching issue for method LocalSessionFactoryBean.setCacheRegionFactory(RegionFactory cacheRegionFactory) -


i`m upgrading project spring-4.1.6.release spring-4.2.1.release , hibernate (core , ehcache) 4.3.9.final 5.0.1.final.

in spring-4.1.6.release, there method

org.springframework.orm.hibernate4.localsessionfactorybean.setcacheregionfactory(regionfactory cacheregionfactory) 

but in spring 4.2.1.release, there no such method in class

org.springframework.orm.hibernate5.localsessionfactorybean 

so when set property in hibernate session factory bean enable second level caching using

<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.ehcacheregionfactory</prop> 

following error thrown:

warn : org.springframework.beans.factory.support.defaultlistablebeanfactory - bean creation exception on factorybean type check: org.springframework.beans.factory.beancreationexception: error creating bean name 'userdao' defined in class path resource [application-context.xml]: cannot resolve reference bean 'transactionmanager' while setting bean property 'transactionmanager'; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'transactionmanager' defined in class path resource [application-context.xml]: cannot resolve reference bean 'sessionfactory' while setting bean property 'sessionfactory'; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'sessionfactory' defined in class path resource [application-context.xml]: error setting property values; nested exception org.springframework.beans.notwritablepropertyexception: invalid property 'cacheregionfactory' of bean class [org.springframework.orm.hibernate5.localsessionfactorybean]: bean property 'cacheregionfactory' not writable or has invalid setter method. parameter type of setter match return type of getter? warn : org.springframework.web.context.support.xmlwebapplicationcontext - exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.beancreationexception: error creating bean name 'homecontroller': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private com.envers.spring.dao.userdao com.envers.spring.homecontroller.userdao; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.envers.spring.dao.userdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} related cause: org.springframework.beans.factory.beancreationexception: error creating bean name 'userdao' defined in class path resource [application-context.xml]: cannot resolve reference bean 'transactionmanager' while setting bean property 'transactionmanager'; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'transactionmanager' defined in class path resource [application-context.xml]: cannot resolve reference bean 'sessionfactory' while setting bean property 'sessionfactory'; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'sessionfactory' defined in class path resource [application-context.xml]: error setting property values; nested exception org.springframework.beans.notwritablepropertyexception: invalid property 'cacheregionfactory' of bean class [org.springframework.orm.hibernate5.localsessionfactorybean]: bean property 'cacheregionfactory' not writable or has invalid setter method. parameter type of setter match return type of getter?     @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:334)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1214)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:543)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482)     @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:305)     @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230)     @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:301)     @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:196)     @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:772)     @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:834)     @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:537)     @ org.springframework.web.servlet.frameworkservlet.configureandrefreshwebapplicationcontext(frameworkservlet.java:667)     @ org.springframework.web.servlet.frameworkservlet.createwebapplicationcontext(frameworkservlet.java:633)     @ org.springframework.web.servlet.frameworkservlet.createwebapplicationcontext(frameworkservlet.java:681)     @ org.springframework.web.servlet.frameworkservlet.initwebapplicationcontext(frameworkservlet.java:552)     @ org.springframework.web.servlet.frameworkservlet.initservletbean(frameworkservlet.java:493)     @ org.springframework.web.servlet.httpservletbean.init(httpservletbean.java:136)     @ javax.servlet.genericservlet.init(genericservlet.java:158)     @ org.apache.catalina.core.standardwrapper.initservlet(standardwrapper.java:1231)     @ org.apache.catalina.core.standardwrapper.loadservlet(standardwrapper.java:1144)     @ org.apache.catalina.core.standardwrapper.load(standardwrapper.java:1031) 

how configure second level caching in spring-4.2.1.release , hibernate-5.0.1.final? code substitution in hibernate-5 enable second level caching?

please on migration issue. in advanced.

tl;dr version: change hibernatetransactionmanager class's package org.springframework.orm.hibernate4 org.springframework.orm.hibernate5.

i able working changing this:

<!-- set default hibernate properties. --> <bean id="hibernateproperties" class="org.springframework.beans.factory.config.propertiesfactorybean">     <property name="properties">         <props>             <prop key="hibernate.dialect">org.hibernate.dialect.h2dialect</prop>             <prop key="hibernate.hbm2ddl.auto">create</prop>             <prop key="hibernate.show_sql">true</prop>             <prop key="hibernate.cache.use_second_level_cache">true</prop>             <prop key="hibernate.cache.use_query_cache">true</prop>         </props>     </property> </bean>  <bean id="cacheregionfactory" class="org.hibernate.cache.ehcache.singletonehcacheregionfactory">     <constructor-arg index="0" ref="hibernateproperties" /> </bean>  <bean id="sessionfactory" class="org.nrg.framework.orm.hibernate.aggregatedannotationsessionfactorybean"       p:cacheregionfactory-ref="cacheregionfactory" p:datasource-ref="datasource"       p:hibernateproperties-ref="hibernateproperties" p:namingstrategy-ref="namingstrategy" />  <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager" p:sessionfactory-ref="sessionfactory" /> 

to this:

<!-- set default hibernate properties. --> <bean id="hibernateproperties" class="org.springframework.beans.factory.config.propertiesfactorybean">     <property name="properties">         <props>             <prop key="hibernate.dialect">org.hibernate.dialect.h2dialect</prop>             <prop key="hibernate.hbm2ddl.auto">create</prop>             <prop key="hibernate.show_sql">true</prop>             <prop key="hibernate.cache.use_second_level_cache">true</prop>             <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.singletonehcacheregionfactory</prop>             <prop key="hibernate.cache.use_query_cache">true</prop>         </props>     </property> </bean>  <bean id="sessionfactory" class="org.nrg.framework.orm.hibernate.aggregatedannotationsessionfactorybean"       p:datasource-ref="datasource" p:hibernateproperties-ref="hibernateproperties" p:physicalnamingstrategy-ref="namingstrategy" />  <bean id="transactionmanager" class="org.springframework.orm.hibernate5.hibernatetransactionmanager" p:sessionfactory-ref="sessionfactory" /> 

the big changes here are:

<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.singletonehcacheregionfactory</prop> 

you've done part.

the other changes are:

  • changing p:namingstrategy-ref p:physicalnamingstrategy-ref (this required change in custom naming implementation, pretty easy)
  • removing singletonehcacheregionfactory bean declaration
  • removing p:cacheregionfactory-ref property setting
  • changing hibernatetransactionmanager class's package org.springframework.orm.hibernate4 org.springframework.orm.hibernate5

i think last 1 critical thing need change.

overall, i'd prefer have type-safe ability set cache region manager through bean rather free-form property value setting, patch on until can figure out way it.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -