2

我正在开发一个 spring hibenrate 应用程序并停留在一个点上。我只有 1 个控制器,只有一项服务。服务更新数据库中的数据并将更新后的 id 返回给客户端并启动线程。

    final Template template = templateBL.getTemplateById(Long.valueOf(templateId));

    Long templateStatusId = templateBL.updateTemplateStatus(template);
    Thread thread = new Thread(templateBL.setTemplate(template, templateStatusId));
    thread.start();
    return templateStatusId;

在 thread.start() 我得到这个错误

Exception in thread "Thread-8" org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:72)
at org.hibernate.impl.SessionImpl.getBatcher(SessionImpl.java:297)
at org.hibernate.loader.Loader.doQuery(Loader.java:770)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2082)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:628)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1853)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:369)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)

我正在使用休眠模板自动装配并在我的 DAO 层上应用@transactional。我的配置是:

 <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <!-- currently set to UPDATE -->
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
 <bean>
 <bean id="transactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
          <property name="sessionFactory" ref="sessionFactory" /> 
 </bean>

我没有得到可能出错的地方。请帮忙。

4

1 回答 1

4

Hibernate 会话不是线程安全的,并且不能被多个线程使用。如果您启动一个新线程,这个新线程应该从会话工厂获得一个新会话。如果将实体传递给新线程,则应首先将实体与会话分离。

否则,正如您的代码所做的那样,任何初始化惰性集合的尝试都将调用会话中的方法。会话将被关闭(从第一个线程),导致您得到异常,或者不会,但是您将在两个线程之间进行共享会话,这将导致各种随机问题。

于 2013-02-04T13:03:45.683 回答