Results 1 to 9 of 9

Thread: a lazyInitalizationException problem

  1. #1
    Join Date
    Apr 2008
    Posts
    13

    Default a lazyInitalizationException problem

    I am trying to use the Struts ,Hibernate and Spring framework in a webapp.I have added the OpenSessenInViewFilter in the web.xml.There is a problem occured.After the user logged on to the system (A User object saved in the session scope) there is a welcome page and in this page I used user.getKnowledges().size() method,then a lazyInitalizationException occured.
    Here is a part of the error message:
    Servlet.service() for servlet jsp threw exception
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.lab1000.project.pojo.Member.knowledges - no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollect ion.throwLazyInitializationException(AbstractPersi stentCollection.java:191)
    at org.hibernate.collection.AbstractPersistentCollect ion.initialize(AbstractPersistentCollection.java:1 83)
    at org.hibernate.collection.AbstractPersistentCollect ion.read(AbstractPersistentCollection.java:48)
    at org.hibernate.collection.PersistentSet.size(Persis tentSet.java:110)
    at org.apache.jsp.admin.welcome_jsp._jspService(welco me_jsp.java:93)
    at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
    at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:393)
    at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:320)
    at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:266)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
    After asking my classmate ,I haved created a class inherited the org.springframework.orm.hibernate3.support.OpenSes sionInViewFilter class and overrided the getSession() method ,before return the session,I haved setted the session.flushMode to be Hibernate.AUTO.
    But the lazyInitalizationException still coccured.
    Last edited by crazysheep; Apr 8th, 2008 at 06:31 AM.

  2. #2
    Join Date
    Apr 2008
    Posts
    13

    Wink Can someone help me?

    Is there something i did not make explicit? Why is there no reply for my question?Someone can help me? I am a chinese college student and I will be very happy to make friends with college student from other countries.

  3. #3

    Default

    The OpenSessionInViewFilter closes the session (associated with the thread handling the HTTP request) at the end of each HTTP request AFAIK (based upon the behaviour of OpenEntityManagerInViewInterceptor which does the same when using JPA and I assume a Hibernate session is equivalent to a EntityManager here).

    A session object has a longer lifetime than a single HTTP request, so when the session is closed at the end of the HTTP request in which the session object was created, you will not be able to fetch any lazy fields in the session object without a lazy initialization exception occurring.

    Solutions to this design problem are -

    1. Eagerly fetch all fields of the object to be stored in the session in the HTTP request that fetches the object from the database.

    2. Don't store the object in the session, store an identifier for the object and fetch the object from the database using the identifier when it's required.

    3. Erm, can't think of a third solution

    Andy.
    Last edited by techfoundry; Apr 20th, 2008 at 07:15 AM.
    ---
    http://www.techfoundry.com for Java development in London, UK.

  4. #4
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Why, 3rd solution is to reattach (or merge) this object.
    Quote Originally Posted by techfoundry View Post
    The OpenSessionInViewFilter closes the session (associated with the thread handling the HTTP request) at the end of each HTTP request AFAIK (based upon the behaviour of OpenEntityManagerInViewInterceptor which does the same when using JPA and I assume a Hibernate session is equivalent to a EntityManager here).

    A session object has a longer lifetime than a single HTTP request, so when the session is closed at the end of the HTTP request in which the session object was created, you will not be able to fetch any lazy fields in the session object without a lazy initialization exception occurring.

    Solutions to this design problem are -

    1. Eagerly fetch all fields of the object to be stored in the session in the HTTP request that fetches the object from the database.

    2. Don't store the object in the session, store an identifier for the object and fetch the object from the database using the identifier when it's required.

    3. Erm, can't think of a third solution

    Andy.

  5. #5
    Join Date
    Dec 2006
    Posts
    6

    Default Configuration problem?

    I had the same issue but I resolved it. It was a configuration problem. Can you attach your configuration files.

  6. #6
    Join Date
    Apr 2008
    Posts
    13

    Default reply

    Thanks for your help.
    And dear prince1540 here is my web.xml configuration file:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/system.xml,/WEB-INF/classes/beans.xml</param-value>
    </context-param>

    <servlet>
    <servlet-name>SpringContextServlet</servlet-name>
    <servlet-class>
    org.springframework.web.context.ContextLoaderServl et
    </servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <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>*.html</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>true</param-value>
    </init-param>
    </filter>

    <filter>
    <filter-name>Set Character Encoding</filter-name>
    <filter-class>com.lab1000.project.system.CharsetEncodingFi lter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>GB2312</param-value>
    </init-param>
    </filter>

    <filter-mapping>
    <filter-name>Set Character Encoding</filter-name>
    <servlet-name>action</servlet-name>
    </filter-mapping>

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

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

  7. #7
    Join Date
    Dec 2006
    Posts
    6

    Default

    Hello crazysheep,

    can you attach system.xml and beans.xml

    Thanks

  8. #8
    Join Date
    Apr 2008
    Posts
    13

    Default reply

    Oh sorry.Here are my spring configuration files:

    system.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>

    <bean id="classficationDao"
    class="com.lab1000.project.dao.impl.ClassficationD aoImpl" abstract="false">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="classficationDaoProxy"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean"
    abstract="false" >
    <property name="target" ref="classficationDao" />
    <property name="transactionManager" ref="txm" />
    <property name="proxyTargetClass" value="true"/>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save">PROPAGATION_REQUIRED</prop>
    <prop key="update">PROPAGATION_REQUIRED</prop>
    <prop key="delete">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="newsDao" class="com.lab1000.project.dao.impl.NewsDaoImpl" abstract="false">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="newsDaoProxy"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean"
    abstract="false" >
    <property name="target" ref="newsDao" />
    <property name="transactionManager" ref="txm" />
    <property name="proxyTargetClass" value="true"/>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save">PROPAGATION_REQUIRED</prop>
    <prop key="update">PROPAGATION_REQUIRED</prop>
    <prop key="delete">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="knowledgeDao"
    class="com.lab1000.project.dao.impl.KnowledgeDaoIm pl" abstract="false">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="knowledgeDaoProxy"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean"
    abstract="false" lazy-init="default" autowire="default"
    dependency-check="default">
    <property name="target">
    <ref bean="knowledgeDao" />
    </property>
    <property name="transactionManager">
    <ref bean="txm" />
    </property>
    <property name="proxyTargetClass">
    <value>true</value>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save">PROPAGATION_REQUIRED</prop>
    <prop key="delete">PROPAGATION_REQUIRED</prop>
    <prop key="update">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="memberDao"
    class="com.lab1000.project.dao.impl.MemberDaoImpl" abstract="false">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="memberDaoProxy"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean"
    abstract="false" >
    <property name="target" ref="memberDao" />
    <property name="transactionManager" ref="txm" />
    <property name="proxyTargetClass" value="true"/>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save">PROPAGATION_REQUIRED</prop>
    <prop key="update">PROPAGATION_REQUIRED</prop>
    <prop key="delete">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="questionDao"
    class="com.lab1000.project.dao.impl.QuestionDaoImp l" abstract="false">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="productDao"
    class="com.lab1000.project.dao.impl.ProductDaoImpl " abstract="false">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    </beans>
    beans.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>

    <bean id="classficationDao"
    class="com.lab1000.project.dao.impl.ClassficationD aoImpl" abstract="false">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="classficationDaoProxy"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean"
    abstract="false" >
    <property name="target" ref="classficationDao" />
    <property name="transactionManager" ref="txm" />
    <property name="proxyTargetClass" value="true"/>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save">PROPAGATION_REQUIRED</prop>
    <prop key="update">PROPAGATION_REQUIRED</prop>
    <prop key="delete">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="newsDao" class="com.lab1000.project.dao.impl.NewsDaoImpl" abstract="false">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="newsDaoProxy"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean"
    abstract="false" >
    <property name="target" ref="newsDao" />
    <property name="transactionManager" ref="txm" />
    <property name="proxyTargetClass" value="true"/>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save">PROPAGATION_REQUIRED</prop>
    <prop key="update">PROPAGATION_REQUIRED</prop>
    <prop key="delete">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="knowledgeDao"
    class="com.lab1000.project.dao.impl.KnowledgeDaoIm pl" abstract="false">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="knowledgeDaoProxy"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean"
    abstract="false" lazy-init="default" autowire="default"
    dependency-check="default">
    <property name="target">
    <ref bean="knowledgeDao" />
    </property>
    <property name="transactionManager">
    <ref bean="txm" />
    </property>
    <property name="proxyTargetClass">
    <value>true</value>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save">PROPAGATION_REQUIRED</prop>
    <prop key="delete">PROPAGATION_REQUIRED</prop>
    <prop key="update">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="memberDao"
    class="com.lab1000.project.dao.impl.MemberDaoImpl" abstract="false">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="memberDaoProxy"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean"
    abstract="false" >
    <property name="target" ref="memberDao" />
    <property name="transactionManager" ref="txm" />
    <property name="proxyTargetClass" value="true"/>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save">PROPAGATION_REQUIRED</prop>
    <prop key="update">PROPAGATION_REQUIRED</prop>
    <prop key="delete">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="questionDao"
    class="com.lab1000.project.dao.impl.QuestionDaoImp l" abstract="false">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="productDao"
    class="com.lab1000.project.dao.impl.ProductDaoImpl " abstract="false">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    </beans>
    Here is the connectionConfig.properties

    connectionConfig.user=root
    connectionConfig.password=007
    connectionConfig.driver=com.mysql.jdbc.Driver
    connectionConfig.url=jdbc:mysql://localhost/labdb
    Here productDaoImpl,newsDaoImpl and the questionDaoImpl are not completed ,so there are no their transactionManagerProxy classes.

  9. #9
    Join Date
    Dec 2006
    Posts
    6

    Default

    So do you any business layer or you are calling daos directly from struts actions. beans.xml and system.xml looks identical? Also you dont have to define transaction proxy each and everytime. Please look below:


    <bean id="abstractTxDefinition"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean"
    lazy-init="true">
    <property name="transactionManager">
    <ref local="mdmTransactionManager" />
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="sampleService1" parent="abstractTxDefinition">
    <property name="target">
    <ref bean="sampleService1Target" />
    </property>
    </bean>

    <bean id="sampleService2" parent="abstractTxDefinition">
    <property name="target">
    <ref bean="sampleService2Target" />
    </property>
    </bean>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •