views:

763

answers:

1

I wired-up but it did not work as I expected. I have set the lazy inialization true between entities abd believe,HibernateInterceptor manages the hibernate session. When I trying to access the object properties, it thorws exception LazyInitializationException.

Error messgae : Transaction could not initialize proxy - the owning Session was closed

Stack Trace: org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:56)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
    at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158)

configuration xml

<!-- THE HIBERNATE INTERCEPTOR -->
<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
 <property name="sessionFactory" ref="app:sessionFactory" />
</bean>

<!-- Start Transaction proxy definition -->

<bean id="app:appTransactionProxy"
 class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
 abstract="true">
 <property name="transactionManager" ref="app:transactionManager" />
 <property name="transactionAttributes">
  <props>
  <prop key="process*">PROPAGATION_REQUIRED,readOnly,
    -Exception,-RuntimeException</prop>
   <prop key="submit*">PROPAGATION_REQUIRED,readOnly,
    -Exception,-RuntimeException</prop>
   <prop key="*">PROPAGATION_REQUIRED,readOnly,
    -Exception,-RuntimeException</prop>
  </props>
 </property>
 <property name="preInterceptors">
  <list>
   <ref bean="hibernateInterceptor" />
  </list>
 </property>
 <property name="postInterceptors">
  <list>
   <ref bean="hibernateInterceptor" />
  </list>
 </property>

</bean>


<bean id="app:daoTransactionProxy"
 class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
 abstract="true">
 <property name="transactionManager" ref="app:transactionManager" />
 <property name="transactionAttributes">
  <props>
   <prop key="unlock*">PROPAGATION_REQUIRES_NEW,
    -Exception,-RuntimeException</prop>
   <prop key="getHead*">PROPAGATION_REQUIRES_NEW,
    -Exception,-RuntimeException</prop>
   <prop key="remove*">PROPAGATION_REQUIRES_NEW,
    -Exception,-RuntimeException</prop>
   <prop key="create*">PROPAGATION_REQUIRES_NEW,
    -Exception,-RuntimeException</prop>
   <prop key="update*">PROPAGATION_REQUIRES_NEW,
    -Exception,-RuntimeException</prop>
   <prop key="addTail*">PROPAGATION_REQUIRES_NEW,
    -Exception,-RuntimeException</prop>
   <prop key="checkForTimeouts">PROPAGATION_REQUIRED,
    -Exception,-RuntimeException</prop>
   <prop key="breakOldLocks">PROPAGATION_REQUIRES_NEW,
    -Exception,-RuntimeException</prop>
   <prop key="find*">PROPAGATION_REQUIRES_NEW,readOnly,
    -Exception,-RuntimeException</prop>
   <prop key="load*">PROPAGATION_REQUIRES_NEW,readOnly,
    -Exception,-RuntimeException</prop>
   <prop key="*">PROPAGATION_REQUIRED,readOnly,
    -Exception,-RuntimeException</prop>

  </props>
 </property>
  <property name="preInterceptors">
  <list>
   <ref bean="hibernateInterceptor" />
  </list>
 </property>
 <property name="postInterceptors">
  <list>
   <ref bean="hibernateInterceptor" />
  </list>
 </property>
</bean>

<bean id="app:transactionManager" name="transactionManager"
 class="org.springframework.orm.hibernate3.HibernateTransactionManager">

 <property name="sessionFactory" ref="app:sessionFactory" />
 <property name="nestedTransactionAllowed" value="true" />
</bean>

    <bean id="app:sessionFactory" name="sessionFactory"
 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

 <property name="dataSource" ref="app:dataSource" />

 <property name="mappingResources">
  <list>
   <value>com/app/domain/app.hbm.xml</value>
   <value>com/common/domain/shared.hbm.xml</value>
  </list>
 </property>

 <property name="hibernateProperties">
  <props>
   <prop key="hibernate.dialect">
    ${app.hibernate.dialect}
   </prop>
   <prop key="hibernate.show_sql">false</prop>
   <prop key="hibernate.generate_statistics">false</prop>
  </props>
 </property>

 <property name="eventListeners">
  <map>
   <entry key="merge">
    <bean
     class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
   </entry>
  </map>
 </property>
</bean>

<!--Start DATA SOURCE   -->
<bean id="app:dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
 destroy-method="close">
 <!-- Config values-->
</bean>

Any input on this ?

A: 

ProxyFactoryBean allows to apply particular logic on the bean methods calling. TransactionProxyFactoryBean IS-A ProxyFactoryBean and it allows to apply transaction-management-specific concerns.

LazyInitializationException means that you access hibernate proxy outside of active hibernate session context.

I.e. you set transaction infrastructure, asked hibernate to get a proxy for particular entity and tried to access proxy properties after tranasction completion.

Solution is to retrieve the object eagerly or work with it from transactional context only.

denis.zhdanov
You are correct but i am using org.springframework.orm.hibernate3.HibernateInterceptor as preInterceptors and postInterceptors which should binds a new Hibernate Session to the thread before a method call, closing and removing it afterwards in case of any method outcome. If there already is a pre-bound Session the interceptor simply participates in it.(http://www.docjar.com/docs/api/org/springframework/orm/hibernate3/HibernateInterceptor.html) am I getting anything wrong here ?
Thillakan
Ok, so you retrieve an entity via hibernate and setup HibernateInterceptor to be applied to that entity or what is the target of your HibernateInterceptor?
denis.zhdanov