Announcement Announcement Module
Collapse
No announcement yet.
OpenSessionInView Interceptor doesn't work with Spring Webflow Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenSessionInView Interceptor doesn't work with Spring Webflow

    OpenSessionInView Interceptor works with Spring MVC and the Hibernate session is avlbl till the view is rendered.

    However, In my application I am using Spring MVC,Spring Webflow and Hibernate, the hibernate session closes before the view is rendered, hence we get Lazy Initialization exception.

    Do we have some other interceptor for this in Spring or we need to handle it manually.

  • #2
    Use the filter instead of the interceptor, if that doesn't work I suggest a read of Spring Web Flow and how to configure/use opensession in flow.

    Comment


    • #3
      Thanks Marten !!..

      I configured OpenSessionInViewFilter and its working fine with with webflows (without any subflows ). In my application, there is a accountSearch.xml flow which has reference to a subflow accountDetails.xml. If app is configured this way, it gives

      java.lang.IllegalStateException: No value for key [org.hibernate.impl.SessionFactoryImpl@9f2588] bound to thread [http-8080-2]
      org.springframework.transaction.support.Transactio nSynchronizationManager.unbindResource(Transaction SynchronizationManager.java:199)
      org.springframework.orm.hibernate3.support.OpenSes sionInViewFilter.doFilterInternal(OpenSessionInVie wFilter.java:206)
      org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)


      But if I configured the accountDetails subflow in the accountSearch Webflow, (i.e I remove the subflow reference), the application
      works fine and does NOT give lazy initialization error.

      Request your help in this please !!!!

      Comment


      • #4
        well, try to use opensession in flow with annotating your webflow with <persistence-context/>. the session is kept open until the flow is finished. i would not recommend mixing opensession in view with spring webflow.

        i'm not sure if you can propagate an open session through a subflow and back. is your subflow requiring an open session. if yes then annotate it with persistence context and let spring manage your session while inside the flow.

        Comment


        • #5
          Open session in view is working for me with in SWF 2.3 with flows and subflows with the next configuration:

          <!-- Creates a flow executor in Spring, responsible for creating and executing flows -->
          <flow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
          <flow:flow-execution-listeners>
          <flow:listener ref="jpaFlowExecutionListener" />
          </flow:flow-execution-listeners>
          </flow:flow-executor>

          <!-- Listener for the JPA execution listener -->
          <bean id="jpaFlowExecutionListener" class="org.springframework.webflow.persistence.Jpa FlowExecutionListener">
          <constructor-arg ref="entityManagerFactory" />
          <constructor-arg ref="transactionManager" />
          </bean>

          <!-- Load flow definitions and make them available to the flow executor -->
          <flow:flow-registry id="flowRegistry">
          <flow:flow-location id="process-flow" path="/process/flows/process-flow.xml" />
          <flow:flow-location id="phase1-flow" path="/process/flows/phase1/phase1-flow.xml" />
          </flow:flow-registry>

          <!-- The FlowHandlerMapping helps DispatcherServlet to knowing that it should send flow requests to Spring Web Flow -->
          <bean class="org.springframework.webflow.mvc.servlet.Flo wHandlerMapping">
          <property name="flowRegistry" ref="flowRegistry" />
          </bean>

          <!-- The FlowHandlerAdapter is equivalent to a Spring MVC controller in that it handles requests coming in for a flow and processes those requests -->
          <bean class="org.springframework.webflow.mvc.servlet.Flo wHandlerAdapter">
          <property name="flowExecutor" ref="flowExecutor" />
          </bean>
          Then I use the tag
          <persistence-context />
          in every flow or subflow configuration XML. Nevertheless it is not working for standard Spring MVC controllers. Does anybody knows how to configure open session in view for working both flows and controllers?

          Regards.

          Comment


          • #6
            hm jpaFlowExecutionListener handles flows persistence. i think you should configure an extra interceptor.

            If you are using Hibernate you can configure it like this.

            https://community.jboss.org/wiki/OpenSessionInView

            To avoid mixing you flow calls and controller calls you should map it to your desired controllers to a specific path(url-pattern)...

            Code:
            <filter-mapping>
                    <filter-name>HibernateFilter</filter-name>
                    <url-pattern>/*</url-pattern> 
            </filter-mapping>
            Or you can use aop...like this
            https://community.jboss.org/wiki/SessionHandlingWithAOP

            Or..better use aop with spring, and bind it to the same transactionhandler...
            Last edited by emil1; Jul 27th, 2012, 07:30 AM.

            Comment


            • #7
              Originally posted by emil1 View Post
              hm jpaFlowExecutionListener handles flows persistence. i think you should configure an extra interceptor.

              If you are using Hibernate you can configure it like this.

              https://community.jboss.org/wiki/OpenSessionInView

              To avoid mixing you flow calls and controller calls you should map it to your desired controllers to a specific path(url-pattern)...

              Code:
              <filter-mapping>
                      <filter-name>HibernateFilter</filter-name>
                      <url-pattern>/*</url-pattern> 
              </filter-mapping>
              Or you can use aop...like this
              https://community.jboss.org/wiki/SessionHandlingWithAOP

              Or..better use aop with spring, and bind it to the same transactionhandler...
              Thank you emil1!!

              Since the first moment I am trying to configure a OpenEntityManagerInViewFilter this way in my web.xml file:

              Code:
              	<filter>
              		<filter-name>oemInViewFilter</filter-name>
              		<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
              		<init-param>
              			<param-name>entityManagerFactoryBeanName</param-name>
              			<param-value>entityManagerFactory</param-value>
              		</init-param>
              	</filter>
              	
              	<filter-mapping>
              		<filter-name>oemInViewFilter</filter-name>
              		<url-pattern>/</url-pattern>
              	</filter-mapping>
              But no way...

              I am using JPA with Hibernate and also have defined the Spring's AOP transaction manager. I have just opened a thread describing my issue http://forum.springsource.org/showth...er-not-working, I hope it helps others with the same scenario than me. Any idea?

              Regards.

              Comment


              • #8
                did you try to apply another url pattern. like
                Code:
                <filter-mapping>
                		<filter-name>oemInViewFilter</filter-name>
                		<url-pattern>/controllers/*</url-pattern>
                </filter-mapping>
                and map your controllers on that "subpath"

                rather than

                Code:
                <filter-mapping>
                		<filter-name>oemInViewFilter</filter-name>
                		<url-pattern>/</url-pattern>
                	</filter-mapping>
                i think its being mixed..take care of this

                map your flows to .../flow/....
                and your controllers /controllers/...
                Last edited by emil1; Jul 27th, 2012, 09:07 AM.

                Comment


                • #9
                  Originally posted by emil1 View Post
                  did you try to apply another url pattern. like
                  Code:
                  <filter-mapping>
                  		<filter-name>oemInViewFilter</filter-name>
                  		<url-pattern>/controllers/*</url-pattern>
                  </filter-mapping>
                  and map your controllers on that "subpath"

                  rather than

                  Code:
                  <filter-mapping>
                  		<filter-name>oemInViewFilter</filter-name>
                  		<url-pattern>/</url-pattern>
                  	</filter-mapping>
                  i think its being mixed..take care of this

                  map your flows to .../flow/....
                  and your controllers /controllers/...
                  Ummm... That makes a lot of sense. I will try this and I tell you further.

                  Thank you for your help and your prompt answer!!

                  Regards.

                  Comment


                  • #10
                    sure, its friday...dont change anything friday for developers :-)

                    Comment


                    • #11
                      Great, it worked!!

                      I have defined the /mvc/* prefix for all my controllers and the flows still reside on /* (as I now, there is no way for setting a custom prefix for flows as you suggested).

                      The SWF configuration would be like this:
                      Code:
                      	<!-- Creates a flow executor in Spring, responsible for creating and executing flows -->
                      	<flow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
                      		<flow:flow-execution-listeners>
                      			<flow:listener ref="jpaFlowExecutionListener" />
                      		</flow:flow-execution-listeners>
                      	</flow:flow-executor>
                      	
                      	<!-- Listener for the JPA execution listener -->
                      	<bean id="jpaFlowExecutionListener" class="org.springframework.webflow.persistence.JpaFlowExecutionListener">
                      	    <constructor-arg ref="entityManagerFactory" />
                      	    <constructor-arg ref="transactionManager" />
                      	</bean>
                      	
                      	<!-- Load flow definitions and make them available to the flow executor -->
                      	<flow:flow-registry id="flowRegistry">
                      		<flow:flow-location id="process-flow" path="/process/flows/process-flow.xml" />
                      		<flow:flow-location id="phase1-flow" path="/process/flows/phase1/phase1-flow.xml" />
                      	</flow:flow-registry>
                      	
                      	<!-- The FlowHandlerMapping helps DispatcherServlet to knowing that it should send flow requests to Spring Web Flow -->
                      	<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
                      		<property name="flowRegistry" ref="flowRegistry" />
                      	</bean>
                      	
                      	<!-- The FlowHandlerAdapter is equivalent to a Spring MVC controller in that it handles requests coming in for a flow and processes those requests -->
                      	<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
                      		<property name="flowExecutor" ref="flowExecutor" />
                      	</bean>
                      And the OpenEntityManagerInViewFilter configuration would be like this:
                      Code:
                      	<filter>
                      		<filter-name>oemInViewFilter</filter-name>
                      		<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
                      		<init-param>
                      			<param-name>entityManagerFactoryBeanName</param-name>
                      			<param-value>entityManagerFactory</param-value>
                      		</init-param>
                      	</filter>
                      	
                      	<filter-mapping>
                      		<filter-name>oemInViewFilter</filter-name>
                      		<url-pattern>/mvc/*</url-pattern>
                      	</filter-mapping>
                      I hope it helps anybody...

                      At the same time, though it works I don't like the solution at all because I don't like to use such pattern for controller's URLs, in my opinion is a little bit ugly (yes, I am too meticolous ). Would be nice to have a single point for configuring open session in view behaviour both for MVC and SWF, and I don't know if such configuration option exists...

                      Anyway thousand of thanks emil1, if I can't find my "perfect solution" I will do it your way .

                      Regards and have a nice weekend.
                      Last edited by alejandrogarciaseco; Jul 27th, 2012, 10:04 AM. Reason: typo

                      Comment

                      Working...
                      X