Announcement Announcement Module
No announcement yet.
Need help with OpenSessionInViewFilter Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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' ''>


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

    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>


    <filter-class> sionInViewFilter




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

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName">
    <property name="url">
    <property name="username">
    <property name="password">
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="mappingDirectoryLocations">
    <property name="hibernateProperties">
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLS erverDialect</prop>
    <property name="dataSource">
    <ref bean="dataSource"/>

    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

    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
            org.hibernate.Session ses = SessionFactoryUtils.getSession&#40;
                    sessionFactory, true&#41;;
            ses.lock&#40;obj, LockMode.NONE&#41;;
            return ses;
        public static void closeHibernateSession&#40;ServletContext sc,org.hibernate.Session ses&#41;
    	   SessionFactory sessionFactory = &#40;SessionFactory&#41;WebApplicationContextUtils.getWebApplicationContext&#40;sc&#41;.getBean&#40;"sessionFactory"&#41;;