错误没有与Hibernate 4和Spring 3绑定到当前线程的会话

时间:2013-12-10 16:50:57

标签: spring hibernate

我正在将我的应用程序更新到Spring 3.2.5和Hibernate 4.2.7。一切都会再次编译,但是当我运行应用程序时,我得到了:

org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:990)
at com.traficon.tmsng.server.common.sequence.SequenceGeneratorServiceImpl$EventIdInitializer.doInitialize_aroundBody0(SequenceGeneratorServiceImpl.java:182)
at com.traficon.tmsng.server.common.sequence.SequenceGeneratorServiceImpl$EventIdInitializer$AjcClosure1.run(SequenceGeneratorServiceImpl.java:1)
at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96cproceed(AbstractTransactionAspect.aj:59)
at org.springframework.transaction.aspectj.AbstractTransactionAspect$AbstractTransactionAspect$1.proceedWithInvocation(AbstractTransactionAspect.aj:65)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96c(AbstractTransactionAspect.aj:63)
at com.traficon.tmsng.server.common.sequence.SequenceGeneratorServiceImpl$EventIdInitializer.doInitialize(SequenceGeneratorServiceImpl.java:179)
at com.traficon.tmsng.server.common.cache.initializer.BaseCoherenceInitializer.initializeIfNeeded(BaseCoherenceInitializer.java:236)
at com.traficon.tmsng.server.common.cache.initializer.BaseCoherenceInitializer.getCache(BaseCoherenceInitializer.java:98)
at com.traficon.tmsng.server.common.sequence.SequenceGeneratorServiceImpl.getHighestEventId(SequenceGeneratorServiceImpl.java:120)
at com.traficon.tmsng.server.common.sequence.SequenceGeneratorServiceImpl.access$0(SequenceGeneratorServiceImpl.java:118)
at com.traficon.tmsng.server.common.sequence.SequenceGeneratorServiceImpl$SequenceGeneratorInitializer.run(SequenceGeneratorServiceImpl.java:149)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

如果查看堆栈跟踪,就会看到事务方面,所以我认为没问题。

我在申请环境中有这个:

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>

我的sessionFactory是org.springframework.orm.hibernate4.LocalSessionFactoryBean

我已经设定:
   hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

知道可能出现什么问题吗?

更新:

似乎只有使用@Transactional(Propagation.SUPPORTS)@Transactional(Propagation.NEVER)注释的方法才能正常工作。如果我将它们更改为Propagation.REQUIRED则可行。但为什么?这曾经在Spring 3.0.4和Hibernate 3.5.6中运行良好。

1 个答案:

答案 0 :(得分:1)

这个问题似乎是一个已知的Spring问题。请参阅https://jira.springsource.org/browse/SPR-9020

解决方法是使用this class并将其设置为Spring SessionFactory中hibernate属性中的hibernate.current_session_context_class(通常应该是org.springframework.orm.hibernate4.LocalSessionFactoryBean