Announcement Announcement Module
Collapse
No announcement yet.
Need help with OpenSessionInViewInterceptor config Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with OpenSessionInViewInterceptor config

    Hi all, i need some help figuring out how to use the OpenSessionInViewInterceptor with Hibernate. The problem is that i get org.hibernate.LazyInitializationException when accessing lazily loaded collections in my Controllers. I've been staring at the configuration files for a while and comparing them with various tutorials and examples, and they look OK to me. I'm using Hibernate 3 and Spring 1.2 rc1 running in a web app in Tomcat 5.5.4.

    First, applicationContext.xml:
    Code:
    <!-- Test datasource -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        varous properties ...
    </bean>
    
    <!-- Hibernate session factory, using annotation configuration -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>classpath&#58;hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>
    
    <bean id="productDao" class="&#91;removed&#93;.ProductDaoImpl">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>
    
    <!-- Transaction Interceptor set up to do PROPAGATION_REQUIRED on all methods -->
    <bean id="matchAllWithPropReq"
        class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource">
        <property name="transactionAttribute"><value>PROPAGATION_REQUIRED</value></property>
     </bean>
    
    <bean id="matchAllTxInterceptor"
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
        <property name="transactionAttributeSource">
            <ref bean="matchAllWithPropReq"/>
        </property>
    </bean>
    
    <!-- One BeanNameAutoProxyCreator handles beans where we want all methods to use PROPAGATION_REQUIRED -->
    <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="interceptorNames">
            <list>
                <value>matchAllTxInterceptor</value>
            </list>
        </property>
        <property name="beanNames">
            <list>
                <idref local="shopDao"/>
            </list>
         </property>
     </bean>
    
    <bean name="openSessionInViewInterceptor"
        class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>
    The openSessionInViewInterceptor bean is used in my handler mapping in the ${webappname}-servlet.xml file:
    Code:
    <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="interceptors">
            <list>
                <ref bean="openSessionInViewInterceptor"/>
            </list>
        </property>
    
        <property name="mappings">
            <props>
               ...
            </props>
        </property>
    </bean>
    My mapped objects are retrieved from DAOs that extend HibernateDaoSupport, e.g.:
    Code:
    public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao &#123;
        public Long create&#40;Product product&#41; &#123;
            return &#40;Long&#41;getHibernateTemplate&#40;&#41;.save&#40;product&#41;;
        &#125;
    
        ...
    &#125;
    Mapped objects are used in the controllers like this:
    Code:
    protected ModelAndView onSubmit&#40;...&#41; &#123;
        ...
        Product product = &#40;Product&#41;command;  // Command object was retrieved from DAO in formBackingObject&#40;&#41;
        product.getTags&#40;&#41;.clear&#40;&#41;;  //  LazyInitializationException thrown here.
        ...
    &#125;
    Here is the relevant portion of the log for a request that causes a LazyInitializationException:
    Code:
    DEBUG DispatcherServlet&#58; DispatcherServlet with name ... received request for &#91;/&#91;removed&#93;/productEdit.do&#93;
    DEBUG DispatcherServlet&#58; Testing handler map &#91;[email protected]412c18&#93; in DispatcherServlet with name '&#91;removed&#93;'
    DEBUG SimpleUrlHandlerMapping&#58; Looking up handler for &#91;/productEdit.do&#93;
    DEBUG OpenSessionInViewInterceptor&#58; Opening single Hibernate session in OpenSessionInViewInterceptor
    DEBUG SessionFactoryUtils&#58; Opening Hibernate session
    DEBUG SessionImpl&#58; opened session
    DEBUG SessionImpl&#58; setting flush mode to&#58; NEVER
    DEBUG TransactionSynchronizationManager&#58; Bound value &#91;org.springframework.orm.hibernate3.SessionHolder@1125f92&#93; for key &#91;org.hibernate.impl.SessionFactoryImpl@17fe89&#93; to thread &#91;http-8080-Processor24&#93;
    DEBUG DispatcherServlet&#58; Testing handler adapter &#91;org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@1f2fbff&#93;
    DEBUG ProductEditController&#58; Removing form session attribute &#91;&#91;removed&#93;.FORM.command&#93;
    DEBUG CachedIntrospectionResults&#58; Using cached introspection results for class &#91;&#91;removed&#93;.Product&#93;
    DEBUG BeanWrapperImpl&#58; About to invoke write method &#91;public void &#91;removed&#93;.Product.setDescription&#40;java.lang.String&#41;&#93; on object of class &#91;&#91;removed&#93;.Product&#93;
    DEBUG BeanWrapperImpl&#58; Invoked write method &#91;public void &#91;removed&#93;.Product.setDescription&#40;java.lang.String&#41;&#93; with value &#91;Purple Cheese Description&#93;
    DEBUG BeanWrapperImpl&#58; About to invoke write method &#91;public void &#91;removed&#93;.Product.setHeadline&#40;java.lang.String&#41;&#93; on object of class &#91;&#91;removed&#93;.Product&#93;
    DEBUG BeanWrapperImpl&#58; Invoked write method &#91;public void &#91;removed&#93;.Product.setHeadline&#40;java.lang.String&#41;&#93; with value &#91;Purple Cheese Headline&#93;
    DEBUG BeanWrapperImpl&#58; About to invoke write method &#91;public void &#91;removed&#93;.Product.setName&#40;java.lang.String&#41;&#93; on object of class &#91;&#91;removed&#93;.Product&#93;
    DEBUG BeanWrapperImpl&#58; Invoked write method &#91;public void &#91;removed&#93;.Product.setName&#40;java.lang.String&#41;&#93; with value &#91;Purple Cheese Name&#93;
    DEBUG BeanWrapperImpl&#58; About to invoke write method &#91;public void &#91;removed&#93;.Product.setProductId&#40;java.lang.String&#41;&#93; on object of class &#91;&#91;removed&#93;.Product&#93;
    DEBUG BeanWrapperImpl&#58; Invoked write method &#91;public void &#91;removed&#93;.Product.setProductId&#40;java.lang.String&#41;&#93; with value &#91;1ABC3&#93;
    DEBUG ProductEditController&#58; No errors -> processing submit
    ERROR LazyInitializationException&#58; failed to lazily initialize a collection &#40;&#91;removed&#93;.Product.tags&#41; - no session or session was closed
    org.hibernate.LazyInitializationException&#58; failed to lazily initialize a collection &#40;&#91;removed&#93;.Product.tags&#41; - no session or session was closed
    	at org.hibernate.collection.AbstractPersistentCollection.initialize&#40;AbstractPersistentCollection.java&#58;179&#41;
    	at org.hibernate.collection.AbstractPersistentCollection.write&#40;AbstractPersistentCollection.java&#58;60&#41;
    	at org.hibernate.collection.PersistentSet.clear&#40;PersistentSet.java&#58;215&#41;
    	at &#91;removed&#93;.product.ProductEditController.onSubmit&#40;ProductEditController.java&#58;78&#41;
    	at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission&#40;SimpleFormController.java&#58;248&#41;
    	at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal&#40;AbstractFormController.java&#58;243&#41;
    	at org.springframework.web.servlet.mvc.AbstractController.handleRequest&#40;AbstractController.java&#58;128&#41;
    	at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle&#40;SimpleControllerHandlerAdapter.java&#58;44&#41;
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch&#40;DispatcherServlet.java&#58;675&#41;
    	at org.springframework.web.servlet.DispatcherServlet.doService&#40;DispatcherServlet.java&#58;623&#41;
    	at org.springframework.web.servlet.FrameworkServlet.serviceWrapper&#40;FrameworkServlet.java&#58;384&#41;
    	at org.springframework.web.servlet.FrameworkServlet.doPost&#40;FrameworkServlet.java&#58;353&#41;
    	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;709&#41;
    	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;802&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;237&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;157&#41;
    	at org.apache.catalina.core.StandardWrapperValve.invoke&#40;StandardWrapperValve.java&#58;214&#41;
    	at org.apache.catalina.core.StandardContextValve.invoke&#40;StandardContextValve.java&#58;178&#41;
    	at org.apache.catalina.core.StandardHostValve.invoke&#40;StandardHostValve.java&#58;126&#41;
    	at org.apache.catalina.valves.ErrorReportValve.invoke&#40;ErrorReportValve.java&#58;105&#41;
    	at org.apache.catalina.valves.AccessLogValve.invoke&#40;AccessLogValve.java&#58;526&#41;
    	at org.apache.catalina.core.StandardEngineValve.invoke&#40;StandardEngineValve.java&#58;107&#41;
    	at org.apache.catalina.connector.CoyoteAdapter.service&#40;CoyoteAdapter.java&#58;148&#41;
    	at org.apache.coyote.http11.Http11Processor.process&#40;Http11Processor.java&#58;825&#41;
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection&#40;Http11Protocol.java&#58;731&#41;
    	at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket&#40;PoolTcpEndpoint.java&#58;526&#41;
    	at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt&#40;LeaderFollowerWorkerThread.java&#58;80&#41;
    	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run&#40;ThreadPool.java&#58;684&#41;
    	at java.lang.Thread.run&#40;Thread.java&#58;595&#41;
    DEBUG TransactionSynchronizationManager&#58; Removed value &#91;org.springframework.orm.hibernate3.SessionHolder@1125f92&#93; for key &#91;org.hibernate.impl.SessionFactoryImpl@17fe89&#93; from thread &#91;http-8080-Processor24&#93;
    DEBUG OpenSessionInViewInterceptor&#58; Closing single Hibernate session in OpenSessionInViewInterceptor
    DEBUG SessionFactoryUtils&#58; Closing Hibernate session
    DEBUG SessionImpl&#58; closing session
    ERROR DispatcherServlet&#58; Could not complete request
    etc.
    Can anyone see what i'm doing wrong? Yes, i know it's Friday

  • #2
    Help pls

    You have in your code handlerMapping bean, ant it have a property "mapping", I don't understand what I must to write there... give a example pls. On the other hand I don't understan how that bean interacts with my DAO class...

    Thanks

    Comment

    Working...
    X