hibernate - Spring MVC OpenSessionInViewInterceptor Not Working -
i have been racking brain awhile on one. had temporarily put aside, it's time pick again. short version this: although have opensessioninviewinterceptor configured in dispatcher-servlet.xml file, still dreaded lazyintializationexception.
now, details.
i have osiv interceptor configured in dispatcher-servlet.xml follows:
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" default-lazy-init="true"> <import resource="applicationcontext.xml" /> <!-- must defined here or dispatcherservlet act bit funky --> <context:component-scan base-package="org.me.app.web.controller" /> <!-- configures @controller programming model --> <mvc:annotation-driven /> <!-- convenient way map urls jsps w/o having controller --> <mvc:view-controller path="/index.html" view-name="index" /> <mvc:view-controller path="/useragreement.html" view-name="useragreement" /> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**/*.html" /> <bean class="org.springframework.orm.hibernate4.support.opensessioninviewinterceptor"> <property name="sessionfactory" ref="sessionfactory" /> </bean> </mvc:interceptor> </mvc:interceptors> the hibernate sessionfactory , transactionmanager beans defined in regular applicationcontext.xml file:
<tx:annotation-driven /> <tx:advice id="transactionadvice" transaction-manager="transactionmanager"> <tx:attributes> <tx:method name="get*" read-only="true" propagation="required"/> <tx:method name="load*" read-only="true" propagation="required"/> <tx:method name="*" propagation="required"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* *.manager.*manager.*(..))" id="serviceoperations"/> <aop:advisor advice-ref="transactionadvice" pointcut-ref="serviceoperations" /> </aop:config> <bean id="sessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean"> <property name="datasource" ref="datasource" /> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.oracle10gdialect</prop> <prop key="hibername.format_sql">true</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.cglib.use_reflection_optimizer">true</prop> </props> </property> <property name="packagestoscan" value="org.me.app.model" /> </bean> <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <property name="datasource" ref="datasource" /> <property name="sessionfactory" ref="sessionfactory" /> </bean> using spring 3.1.1 , hibernate 4.1.
any appreciated.
jason
edit: on suggestion, attempted switch filter rather interceptor. commented out entire section of dispatch-servlet.xml , added following web.xml (with other filters , filter mappings shown well):
<context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:applicationcontext.xml classpath:security.xml </param-value> </context-param> <filter> <filter-name>osiv</filter-name> <filter-class>org.springframework.orm.hibernate4.support.opensessioninviewfilter</filter-class> </filter> <filter> <filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.module.sitemesh.filter.pagefilter</filter-class> </filter> <filter> <filter-name>springsecurityfilterchain</filter-name> <filter-class>org.springframework.web.filter.delegatingfilterproxy</filter-class> </filter> <!-- filter checks see if user has accepted terms --> <filter> <filter-name>accepttermsfilter</filter-name> <filter-class>org.me.web.filter.terms.accepttermsfilter</filter-class> </filter> <filter-mapping> <filter-name>springsecurityfilterchain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>osiv</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>accepttermsfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener>
i assuming using osiv expanding out relation in jsp view - if case interceptor may not work, interceptors complete handling before view rendered, in specific case before jsp rendered container hibernate session have been closed.
a better approach may use osiv filter - org.springframework.orm.hibernate4.support.opensessioninviewfilter support lazy relations in jsp also.
update: 1 thing noticed have applicationcontext.xml imported in dispatcher-servlet.xml file, better not import applicationcontext.xml in dispatcher-servlet.xml, instead load using contextloaderlistener:
<listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath*:meta-inf/spring/applicationcontext*.xml</param-value> </context-param> and load rest of dispatcher-servlet.xml normally. reason saying route root application context registered singleton , available hibernate sessionfactory detected osiv filter. can please try , see how goes
Comments
Post a Comment