2009-11-26 5 views
0

J'ai câblé mais ça n'a pas fonctionné comme je m'y attendais. J'ai mis l'inialisation paresseux vrai entre les entités et je crois, HibernateInterceptor gère la session d'hibernation. Lorsque j'essaie d'accéder aux propriétés de l'objet, il dispense l'exception LazyInitializationException.Comment puis-je câbler org.springframework.transaction.interceptor.TransactionProxyFactoryBean et org.springframework.orm.hibernate3.HibernateInterceptor?

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> 

Toute entrée sur ce sujet?

+1

Où se trouve votre 'application: transactionManager'? – Bozho

+0

Oui, j'ai ajouté 'app: transactionManager' – nayakam

Répondre

0

ProxyFactoryBean permet d'appliquer une logique particulière sur l'appel des méthodes du bean. TransactionProxyFactoryBean IS-A ProxyFactoryBean et permet d'appliquer des problèmes spécifiques à la gestion des transactions.

LazyInitializationException signifie que vous accédez en veille prolongée proxy en dehors du contexte de la session de mise en veille prolongée active.

I.e. vous avez défini l'infrastructure de transaction, demandé à hibernate d'obtenir un proxy pour une entité particulière et essayé d'accéder aux propriétés du proxy après l'achèvement de la transaction.

La solution consiste à récupérer l'objet avec empressement ou à travailler avec celui-ci uniquement à partir du contexte transactionnel.

+1

Vous avez raison, mais j'utilise org.springframework.orm.hibernate3.HibernateInterceptor comme preInterceptors et postInterceptors qui devrait lier une nouvelle session Hibernate au thread avant un appel de méthode, la fermeture et la suppression après dans le cas de n'importe quel résultat de méthode. S'il y a déjà une session pré-liée, l'intercepteur y participe simplement. (Http://www.docjar.com/docs/api/org/springframework/orm/hibernate3/HibernateInterceptor.html) Est-ce que je reçois quelque chose de mal ici? – nayakam

+0

Ok, donc vous récupérez une entité via Hibernate et configurez HibernateInterceptor à appliquer à cette entité ou quelle est la cible de votre HibernateInterceptor? –

Questions connexes