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

  • Need help with OpenSessionInViewFilter

    I have read all the posts and blogs on this subject and I have implemented it to the best of my knowledge but it does not work. I still get the org.hibernate.LazyInitializationException.

    Here's my web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>

    <web-app>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/tess-servlet.xml
    </param-value>
    </context-param>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>

    <servlet>
    <servlet-name>tess</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>tess</servlet-name>
    <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>
    org.springframework.orm.hibernate3.support.OpenSes sionInViewFilter
    </filter-class>
    <init-param>
    <param-name>singleSession</param-name>
    <param-value>false</param-value>
    </init-param>
    </filter>

    <filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>*.htm</url-pattern>
    </filter-mapping>

    <welcome-file-list>
    <welcome-file>
    index.jsp
    </welcome-file>
    </welcome-file-list>

    </web-app>

    Here's the snippet from my tess-servlet.xml

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName">
    <value>net.sourceforge.jtds.jdbc.Driver</value>
    </property>
    <property name="url">
    <value>jdbc:jtds:sqlserver://localhost/company</value>
    </property>
    <property name="username">
    <value>sa</value>
    </property>
    <property name="password">
    <value>password</value>
    </property>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="mappingDirectoryLocations">
    <list>
    <value>classpath:/com/company/tess/objects</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLS erverDialect</prop>
    </props>
    </property>
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    </bean>

    One thing I have noticed is that the log file shows all the hibernate mappings happening twice.

    Any ideas would be appreciated.

  • #2
    You have to understand that the OpenSessionIn View filter is good only for that request. I assume you are accessing a dao which returns an entity object. Once the servlet request is over the session gets closed.

    Let us say that you are retrieving an entity in a request don't call any associated method but persist your object in your session. At some other point you retrieve the object from your session then access the associated method you are sure to get the LazyInit error. The way to avoid that scenario is to add the below two methods to your utility class and call reassociate to attach the detatched object to the hibernate session and once your are done close the session.

    Also if you are calling a session bean which access your dao then you will get the LazyInit error even if you set the filter. OpenSessionInView Filter keeps the session open for the duration of the request.

    Read Karl Baum's weblog on this topic

    Code:
    public static org.hibernate.Session reassociateEntity&#40;ServletContext sc,
                Object obj&#41; &#123;
    
            // Remember to call sessionFactoryUtils.closeSessionIfNecessary&#40;
    
            SessionFactory sessionFactory = &#40;SessionFactory&#41; WebApplicationContextUtils
                    .getWebApplicationContext&#40;sc&#41;.getBean&#40;"sessionFactory"&#41;;
            org.hibernate.Session ses = SessionFactoryUtils.getSession&#40;
                    sessionFactory, true&#41;;
            ses.lock&#40;obj, LockMode.NONE&#41;;
    
            return ses;
        &#125;
    
        public static void closeHibernateSession&#40;ServletContext sc,org.hibernate.Session ses&#41;
        &#123;
    
    
    	   SessionFactory sessionFactory = &#40;SessionFactory&#41;WebApplicationContextUtils.getWebApplicationContext&#40;sc&#41;.getBean&#40;"sessionFactory"&#41;;
    	   SessionFactoryUtils.closeSessionIfNecessary&#40;ses,sessionFactory&#41;;
    
        &#125;

    Comment

    Working...
    X