Announcement Announcement Module
Collapse
No announcement yet.
How to use OpenSessionInViewFilter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use OpenSessionInViewFilter

    Hi everyone,

    I'm using the following frameworks for my web application:

    Hibernate 3.0.5
    Spring 1.2.5
    Struts 1.2.7
    Tomcat 5.0.28

    I tried to set up OpenInSessionViewFilter to access my persistent collections lazily in my views. Unfortunately, it seems i didn't configure my web.xml correctly as i always get this famous error :
    org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed



    This is how I configured my xml files:

    web.xml (sorry for the indentation)
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/application-context.xml</param-value>
    </context-param>
    <filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.O penSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>*.do</url-pattern>
    </filter-mapping>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>
    <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
    <param-name>config</param-name>
    <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
    <param-name>debug</param-name>
    <param-value>3</param-value>
    </init-param>
    <init-param>
    <param-name>detail</param-name>
    <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
    <welcome-file>/welcome.jsp</welcome-file>
    </welcome-file-list>

    struts-config.xml
    <struts-config>
    <form-beans>
    ...
    </form-beans>

    <global-exceptions />

    <global-forwards>
    <forward name="portal" path="/index.jsp" />
    <forward name="loginPage" path="/login.jsp"/>
    </global-forwards>

    <action-mappings>
    <action path="/toModule" type="org.springframework.web.struts.DelegatingAct ionProxy" />

    <action path="/welcome" type="org.springframework.web.struts.DelegatingAct ionProxy" />
    ...
    </action-mappings>

    <controller processorClass="myApp.struts.main.RequestProcessor " />

    <message-resources parameter="myApp.struts.ApplicationResources" />


    <plug-in className="org.springframework.web.struts.ContextL oaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/application-context.xml" />
    </plug-in>
    </struts-config>

    application-context.xml
    <beans>

    <import resource="controller.xml" />
    <import resource="service.xml" />
    <import resource="dao.xml" />
    <import resource="persistence.xml" />
    </beans>

    i won't include all the xml as they are just mapping of beans. I'ill just focus on persistence.xml

    persistence.xml
    <beans>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean" singleton="true">
    <property name="dataSource">
    <ref local="dataSource" />
    </property>
    <property name="mappingResources">
    ...
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQ LDialect</prop>
    <prop key="hibernate.show_sql">false</prop>
    </props>
    </property>
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName">
    <value>java:/comp/env/myDS</value>
    </property>
    </bean>
    </beans>

    It seems my hibernateFilter that i have defined just doesn't work because of a mistake in my configuration...


    Would you mind telling my what's wrong with my configuraion of OpenSessionInViewFilter ?
    Did I forget something ?


    Thank you in advance for your help.

    Best Regards,

    Jay
    Last edited by JayJayBee; Oct 29th, 2005, 05:30 AM.

  • #2
    Just by looking over your configurations, I cannot see anything wrong. When the error occurs, what's the request url in the browser? What's the stack trace?

    -karl

    Comment


    • #3
      Thanks for your comments,


      my url end with showSubmissionForm.do. Everything seems normal here

      Though, i'd like to point out that i'm using tiles to render my page. Does this has a role in my problem?

      This is the error trace:
      GRAVE: could not initialize proxy - the owning Session was closed
      org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
      at org.hibernate.proxy.AbstractLazyInitializer.initia lize(AbstractLazyInitializer.java:53)
      at org.hibernate.proxy.AbstractLazyInitializer.getImp lementation(AbstractLazyInitializer.java:84)
      at org.hibernate.proxy.CGLIBLazyInitializer.intercept (CGLIBLazyInitializer.java:134)
      at myPackage.Person$$EnhancerByCGLIB$$c7ee8950.getFir stname(<generated>)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at org.apache.commons.beanutils.PropertyUtilsBean.inv okeMethod(PropertyUtilsBean.java:1773)
      at org.apache.commons.beanutils.PropertyUtilsBean.get SimpleProperty(PropertyUtilsBean.java:1132)
      at org.apache.commons.beanutils.PropertyUtilsBean.get NestedProperty(PropertyUtilsBean.java:686)
      at org.apache.commons.beanutils.PropertyUtilsBean.get Property(PropertyUtilsBean.java:715)
      at org.apache.commons.beanutils.PropertyUtils.getProp erty(PropertyUtils.java:290)
      at org.apache.struts.taglib.TagUtils.lookup(TagUtils. java:950)
      at org.apache.struts.taglib.bean.WriteTag.doStartTag( WriteTag.java:225)
      at org.apache.jsp.IdeaSubmission.ideaSubmissionForm_j sp._jspx_meth_bean_write_4(ideaSubmissionForm_jsp. java:1302)
      at org.apache.jsp.IdeaSubmission.ideaSubmissionForm_j sp._jspService(ideaSubmissionForm_jsp.java:503)
      at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:94)
      at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
      at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:324)
      at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:292)
      at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:236)
      at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
      at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:237)
      at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
      at org.apache.catalina.core.ApplicationDispatcher.inv oke(ApplicationDispatcher.java:704)
      at org.apache.catalina.core.ApplicationDispatcher.doI nclude(ApplicationDispatcher.java:590)
      at org.apache.catalina.core.ApplicationDispatcher.inc lude(ApplicationDispatcher.java:510)
      at org.apache.jasper.runtime.JspRuntimeLibrary.includ e(JspRuntimeLibrary.java:966)
      at org.apache.jasper.runtime.PageContextImpl.include( PageContextImpl.java:581)
      at org.apache.struts.tiles.TilesUtilImpl.doInclude(Ti lesUtilImpl.java:99)
      at org.apache.struts.tiles.TilesUtil.doInclude(TilesU til.java:135)
      at org.apache.struts.taglib.tiles.InsertTag.doInclude (InsertTag.java:760)
      at org.apache.struts.taglib.tiles.InsertTag$InsertHan dler.doEndTag(InsertTag.java:892)
      at org.apache.struts.taglib.tiles.InsertTag.doEndTag( InsertTag.java:462)
      at org.apache.jsp.layout.formLayout_jsp._jspx_meth_ti les_insert_1(formLayout_jsp.java:182)
      at org.apache.jsp.layout.formLayout_jsp._jspx_meth_ht ml_html_0(formLayout_jsp.java:122)
      at org.apache.jsp.layout.formLayout_jsp._jspService(f ormLayout_jsp.java:74)
      at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:94)
      at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
      at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:324)
      at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:292)
      at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:236)
      at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
      at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:237)
      at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
      at org.apache.catalina.core.ApplicationDispatcher.inv oke(ApplicationDispatcher.java:704)
      at org.apache.catalina.core.ApplicationDispatcher.doI nclude(ApplicationDispatcher.java:590)
      at org.apache.catalina.core.ApplicationDispatcher.inc lude(ApplicationDispatcher.java:510)
      at org.apache.jasper.runtime.JspRuntimeLibrary.includ e(JspRuntimeLibrary.java:966)
      at org.apache.jasper.runtime.PageContextImpl.include( PageContextImpl.java:581)
      at org.apache.struts.tiles.TilesUtilImpl.doInclude(Ti lesUtilImpl.java:99)
      at org.apache.struts.tiles.TilesUtil.doInclude(TilesU til.java:135)
      at org.apache.struts.taglib.tiles.InsertTag.doInclude (InsertTag.java:760)
      at org.apache.struts.taglib.tiles.InsertTag$InsertHan dler.doEndTag(InsertTag.java:892)
      at org.apache.struts.taglib.tiles.InsertTag.doEndTag( InsertTag.java:462)
      at org.apache.jsp.IdeaSubmission.ideaSubmissionFormPa ge_jsp._jspx_meth_tiles_insert_0(ideaSubmissionFor mPage_jsp.java:115)
      at org.apache.jsp.IdeaSubmission.ideaSubmissionFormPa ge_jsp._jspService(ideaSubmissionFormPage_jsp.java :69)
      at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:94)
      at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
      at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:324)
      at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:292)
      at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:236)
      at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
      at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:237)
      at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
      at org.apache.catalina.core.ApplicationDispatcher.inv oke(ApplicationDispatcher.java:704)
      at org.apache.catalina.core.ApplicationDispatcher.pro cessRequest(ApplicationDispatcher.java:474)
      at org.apache.catalina.core.ApplicationDispatcher.doF orward(ApplicationDispatcher.java:409)
      at org.apache.catalina.core.ApplicationDispatcher.for ward(ApplicationDispatcher.java:312)
      at org.apache.struts.action.RequestProcessor.doForwar d(RequestProcessor.java:1063)
      at org.apache.struts.action.RequestProcessor.processF orwardConfig(RequestProcessor.java:386)
      at org.apache.struts.action.RequestProcessor.process( RequestProcessor.java:229)
      at org.apache.struts.action.ActionServlet.process(Act ionServlet.java:1194)
      at org.apache.struts.action.ActionServlet.doGet(Actio nServlet.java:414)
      at javax.servlet.http.HttpServlet.service(HttpServlet .java:689)
      at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
      at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:237)
      at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
      at org.springframework.orm.hibernate3.support.OpenSes sionInViewFilter.doFilterInternal(OpenSessionInVie wFilter.java:174)
      at org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)
      at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:186)
      at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
      at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:214)
      at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:520)
      at org.apache.catalina.core.StandardContextValve.invo keInternal(StandardContextValve.java:198)
      at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:152)
      at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:520)
      at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:137)
      at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:104)
      at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:118)
      at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:520)
      at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
      at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:520)
      at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:929)
      at org.apache.coyote.tomcat5.CoyoteAdapter.service(Co yoteAdapter.java:160)
      at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:799)
      at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.processConnection(Http11Protocol.jav a:705)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(P oolTcpEndpoint.java:577)
      at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Thread.java:534)


      Thank you in advance for your help

      Comment


      • #4
        Well I tried without tiles but it doesn't work... So that was not the cause...

        Any idea?

        Thanks

        Comment


        • #5
          Hi,

          I'd crank up the debug logging to see if the filter is actually running. You never know.

          OSIVFilter will print these two lines:

          Opening single Hibernate Session in OpenSessionInViewFilter

          Closing single Hibernate Session in OpenSessionInViewFilter

          See if you see those, and in the place you expect.

          Seth

          Comment


          • #6
            Struts and Spring OpenSessionInViewFilter

            I think that my problem comes from this reason :

            In order to define the OpenSessionInViewFilter, i have to load a spring context. I have to do it through the definition, in web.xml of a listener. (ContextLoaderListener).
            The trouble is that i have to define once again my context for struts. Indeed, in struts-config.xml, i have to register an application context thank to ContextLoaderPlugin.

            If i don't define both contexts, i have exceptions (no context registered).
            Though, by defining twice my context, then, i see in logs that my contexts are actually loaded twice... Maybe that would explain why OpenSessionInViewFilter cannot retrieve the sessionFactory as there are 2 different contexts loaded...

            My Question is then : How to define a unic context for struts and my OpenSessionInViewFilter ? Is it possible ? Will it solve my problem?

            Thank you in advance for your help,

            Best Regards

            Jay

            Comment


            • #7
              Problem solved

              MY PROBLEM IS SOLVED !

              In fact, the last reason i invoked was the right one : Defining twice the context made the OpenSessionInViewFilter not work correctly. What i did to tackle with the problem was:
              1. Loading all my context files through the ContextLoaderListener in web.xml except my struts actions mapping.
              2. Loading only my struts actions mappings with the struts/spring plug-in

              Now, sessions are opened in my views and properly closed !

              Thank Seth and Karl for your precious hints ! That guided me while debugging my issue !

              BYe

              Jay

              Comment

              Working...
              X