Java 自动连线休眠模板在线程中使用时丢失会话
我正在开发一个spring hibenrate应用程序,并且在某个点上卡住了。 我只有一个控制器,只有一个服务。服务更新数据库中的数据,并将更新后的id返回给客户端,然后启动线程Java 自动连线休眠模板在线程中使用时丢失会话,java,multithreading,spring,hibernate,sessionfactory,Java,Multithreading,Spring,Hibernate,Sessionfactory,我正在开发一个spring hibenrate应用程序,并且在某个点上卡住了。 我只有一个控制器,只有一个服务。服务更新数据库中的数据,并将更新后的id返回给客户端,然后启动线程 final Template template = templateBL.getTemplateById(Long.valueOf(templateId)); Long templateStatusId = templateBL.updateTemplateStatus(template);
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层上使用hibernate模板自动连接并应用@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>
我没有得到可能出错的东西。请提供帮助。Hibernate会话不安全,不能由多个线程使用。如果启动一个新线程,这个新线程应该从会话工厂获得一个新会话。如果将实体传递给新线程,则应首先将该实体与会话分离
否则,正如代码所做的那样,任何初始化惰性集合的尝试都将从会话调用方法。会话将从第一个线程关闭,导致出现异常,或者不会,但随后两个线程之间将有一个共享会话,这将导致各种随机问题。Hibernate会话不安全,不能由多个线程使用。如果启动一个新线程,这个新线程应该从会话工厂获得一个新会话。如果将实体传递给新线程,则应首先将该实体与会话分离
否则,正如代码所做的那样,任何初始化惰性集合的尝试都将从会话调用方法。要么会话将从第一个线程关闭,导致出现异常,要么不会,但两个线程之间将有一个共享会话,这将导致各种各样的随机问题。事务管理器在哪里?能否显示更多代码,例如sqlqueries@ArunPJohny@AlekseiBulgak hibernateTemplate.savetemplateStatus;TemplateStatus TemplateStatus=TemplateStatus hibernateTemplate.getSessionFactory.getCurrentSession.getTemplateStatus.class,id;结果=hibernateTemplate.findFROM_TEMPLATE_STATUS+StringWhere子句;我确信我丢失了线程所需的一些配置,当我在没有线程的情况下使用它时,应用程序在没有任何错误的情况下成功运行。事务管理器在哪里?您可以显示更多代码,例如sql吗queries@ArunPJohny@AlekseiBulgak hibernateTemplate.savetemplateStatus;TemplateStatus TemplateStatus=TemplateStatus hibernateTemplate.getSessionFactory.getCurrentSession.getTemplateStatus.class,id;结果=hibernateTemplate.findFROM_TEMPLATE_STATUS+StringWhere子句;我确信我缺少了线程所需的一些配置,当我在没有线程的情况下使用它时,应用程序在没有任何错误的情况下成功运行,但是我自动连接hibernate模板,我想它将自己管理会话@自动连线HibernateTemplate HibernateTemplate;是的,但看起来您正在将附加的实体从一个线程传递到另一个线程。附加的实体链接到在第一个线程中打开的会话。@HimanshuArora请将其标记为已应答,这样它就不会出现在应答JB的未应答部分Thanx中,而是自动连接hibernate模板,我想它将自行管理会话@自动连线HibernateTemplate HibernateTemplate;是的,但看起来您正在将附加的实体从一个线程传递到另一个线程。附加的实体链接到在第一个线程中打开的会话。@HimanshuArora请将其标记为已回答,以便它不会显示在“未回答”部分中