Announcement Announcement Module
Collapse
No announcement yet.
OpenSessionInViewFilter + UrlRewrite seems not to work properly Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenSessionInViewFilter + UrlRewrite seems not to work properly

    Hi, I'm using Springsource Tool Suite 2.1.0.SR01, and I've created New Project based on Spring Template Project (Spring MVC Project).

    This project contains standard settings like installed UrlRewriteFilter, and now I want to add a Hibernate to it.

    I've created DAO for entities like City, Region, Country (and now city contains reference to Region, and Region contains reference to Country). I want to read City and display Region name on page.

    Region is lazy fetched so I want to use OpenSessionInViewFilter to get the RegionEntity in view. I've got configured data source and session factory for Hibernate, and I prepared web.xml configuration to use OpenSessionInViewFilter, but when I run application it seems not to work (because I get LazyInitializationException). I tried to switch order of filters but I only get a different error. Can you help me?

    Changing fetch type to eager is not an option :/.

    Thanks in advance.


    P.S. I put detailed errors in txt file (it's zipped because it was to big).


    This is my configuration:

    urlrewrite.xml //I've made no changes in it.
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
    <urlrewrite default-match-type="wildcard">
    	<rule>
    		<from>/</from>
    		<to>/app/welcome</to>
    	</rule>
    	<rule>
    		<from>/**</from>
    		<to>/app/$1</to>
    	</rule>
    	<outbound-rule>
    		<from>/app/**</from>
    		<to>/$1</to>
    	</outbound-rule>
    </urlrewrite>
    app-config.xml
    Code:
    <?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"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    		http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    	<!--
    		Scans within the base package of the application for @Components to
    		configure as beans
    	-->
    	<context:component-scan base-package="pl.biotechnologia.biznes.controller" />
    
    	<bean id="myDataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost:3306/db5273" />
    		<property name="username" value="root" />
    		<property name="password" value="root" />
    	</bean>
    
    	<bean id="mySessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="myDataSource" />
    		<property name="hibernateProperties">
    			<value>
    				hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
    				hibernate.show_sql=true
                </value>
    		</property>
    		<property name="mappingDirectoryLocations">
    			<value>WEB-INF/mappings</value>
    		</property>
    	</bean>
    	
    </beans>
    And my web.xml config file

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
    	<!-- Enables clean URLs with JSP views e.g. /welcome instead of /app/welcome -->
    	<filter>
    		<filter-name>UrlRewriteFilter</filter-name>
    		<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>	
    	</filter>
    
    	<filter-mapping>
    		<filter-name>UrlRewriteFilter</filter-name>
    		<url-pattern>/*</url-pattern>		
    	</filter-mapping>	
     
    	<!-- Session in View filter -->	
        <filter>
            <filter-name>sessionInViewFilter</filter-name>
            <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
            <init-param>
                <param-name>singleSession</param-name>
                <param-value>false</param-value>
            </init-param>
            <init-param>
                <param-name>sessionFactoryBeanName</param-name>
                <param-value>mySessionFactory</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>sessionInViewFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>	
    		
    	<!-- Handles all requests into the application -->
    	<servlet>
    		<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>
    				/WEB-INF/spring/*.xml
    			</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    		
    	<!-- Maps all /app requests to the DispatcherServlet for handling -->
    	<servlet-mapping>
    		<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    		<url-pattern>/app/*</url-pattern>
    	</servlet-mapping>
    	
    </web-app>
    Finally my errors: (in web.xml there is urlRewriteFilter then OpenSessionInView)
    Code:
    ERROR: org.hibernate.LazyInitializationException - 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.initialize(AbstractLazyInitializer.java:60)
    	at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
    	at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:140)
    	at pl.biotechnologia.biznes.model.RegionEntity$$EnhancerByCGLIB$$de989cbe.getName(<generated>)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    and Error I get when I switch order of filters in web.xml file (in web.xml there is OpenSessionInView then urlRewriteFilter)

    Code:
    2009-09-15 13:00:58 org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet default threw exception
    java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
    	at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApp
    licationContextUtils.java:70)
    	at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewF
    ilter.java:241)
    	at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewF
    ilter.java:227)
    	at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilte
    r.java:171)
    	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at ...

  • #2
    I didn't found solution to my problem. I still don't know why this Spring configuration from STS project doesn't work, so I decided to rewrite configuration all by myself.

    And it works perfectly now.

    Comment


    • #3
      Me too

      Im getting the same error, anyone solve this problem?

      Comment


      • #4
        Now Works!

        Well seems that STS does not configure a ContextLoaderListener.

        So I have to add this:

        Code:
        <!-- Bootstraps the root web application context before servlet initialization -->
          	<context-param>
        		<param-name>contextConfigLocation</param-name>
        		<param-value>/WEB-INF/spring/app-config.xml</param-value>
        	</context-param>
        	<listener>
        		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        	</listener>

        Comment

        Working...
        X