OpenSessionInView过滤器从Spring 2.5到Spring 4的转换

时间:2016-08-17 19:48:26

标签: spring hibernate spring-mvc

我正在将使用Spring 2.5的旧SpringMVC应用程序转换为Spring 4.我无法使OpenSessionInView过滤器正常工作,这在2.5中运行良好。

我的情况是我有一个命令对象,在Request GET中返回,然后存储在Session中。此表单已发布。然后,某些服务层代码尝试从最初未提取的命令对象中读取集合,从而导致LazyInitializationException

我不断得到的错误是:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.abc.model.Entity1.details, could not initialize proxy - no Session

Web.xml中:

<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>sessionFactory</param-value>
    </init-param>
</filter>

....

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

hibernateFilter首先列在<filter-mapping>订单中。

在我的Hibernate.Xml中,我得到了以下内容:

<alias name="abcSessionFactory" alias="commonSessionFactory"/>
<alias name="abcSessionFactory" alias="instrSessionFactory"/>
<alias name="abcSessionFactory" alias="sessionFactory"/>

<!-- Hibernate SessionFactory -->
<bean id="abcSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="abcDataSource"/>
    <property name="mappingLocations">
        <list>
            <value>classpath*:/org/abc/model/*.xml</value>
            <value>classpath*:/org/abc/common/model/*.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
        </props>
    </property>
</bean>

<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="abcSessionFactory"/>
    <property name="nestedTransactionAllowed" value="true"/>
</bean>

<alias name="txManager" alias="transactionManager"/>

我认为配置必须有更多,但我不知所措。

我尝试使用hibernate.enable_lazy_load_no_trans属性,这确实有效。但感觉就像这样一个完全黑客,所以我真的想弄明白如何让OpenSessionInViewFilter工作。

编辑:

来自OpenSessionInViewFilter:

    SessionFactory sessionFactory = lookupSessionFactory(request);
    boolean participate = false;

    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    String key = getAlreadyFilteredAttributeName();

    if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
        // Do not modify the Session: just set the participate flag.
        participate = true;
    }
    else {
        boolean isFirstRequest = !isAsyncDispatch(request);
        if (isFirstRequest || !applySessionBindingInterceptor(asyncManager, key)) {
            logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");
            Session session = openSession(sessionFactory);
            SessionHolder sessionHolder = new SessionHolder(session);
            TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);

            AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(sessionFactory, sessionHolder);
            asyncManager.registerCallableInterceptor(key, interceptor);
            asyncManager.registerDeferredResultInterceptor(key, interceptor);
        }
    }

在此处跟踪,session对象已创建,似乎没问题。

稍后在我的Controller代码中调用PersistentSet代码以尝试加载有问题的集合。来自AbstractPersistentCollection

private <T> T withTemporarySessionIfNeeded(LazyInitializationWork<T> lazyInitializationWork) {
    SessionImplementor originalSession = null;
    boolean isTempSession = false;
    boolean isJTA = false;

    if ( session == null ) {
        if ( allowLoadOutsideTransaction ) {
            session = openTemporarySessionForLoading();
            isTempSession = true;
        }
        else {
            throwLazyInitializationException( "could not initialize proxy - no Session" );
        }
    }
    else if ( !session.isOpen() ) {

...
...

session为空,allowLoadOutsideTransactionfalse。这就是我得到的例外情况。

我不熟悉session是否应该在此处为空,或者allowOutsideTransaction是否有问题。

1 个答案:

答案 0 :(得分:0)

OpenSessionInViewFilter保留会话以满足请求的范围。你在谈论保留两个请求,这是有问题的。

您应该从休眠中重新检索对象,或者您可以在GET请求处理期间evict对象,然后update它在POST期间将其“重新附加”到会话。 / p>