我如何连接org.springframework.transaction.interceptor.TransactionProxyFactoryBean和org.springframework.orm.hibernate3.HibernateInterceptor?

时间:2009-11-26 23:59:23

标签: hibernate spring

我接线了,但它没有像我预期的那样工作。 我在实体abd相信之间设置了懒惰的inialization true,HibernateInterceptor管理hibernate会话。当我尝试访问对象属性时,它会抛出异常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)

配置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>

关于此的任何输入?

1 个答案:

答案 0 :(得分:0)

ProxyFactoryBean 允许在bean方法调用上应用特定逻辑。 TransactionProxyFactoryBean IS-A ProxyFactoryBean ,它允许应用特定于交易管理的问题。

LazyInitializationException 表示您在活动的hibernate会话上下文之外访问hibernate代理。

即。你设置了事务基础结构,要求hibernate获取特定实体的代理,并在tranasction完成后尝试访问代理属性。

解决方案是热切地检索对象或仅从事务上下文中使用它。