1

我收到以下错误:

    org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at com.test.serviceimpl.EbayCredentialImpl.getCredential(EbayCredentialImpl.java:22)
at com.test.serviceimpl.EbayCredentialImpl$$FastClassBySpringCGLIB$$3f81c256.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at com.test.serviceimpl.EbayCredentialImpl$$EnhancerBySpringCGLIB$$a9d77df8.getCredential(<generated>)
at com.test.jobs.EbayJob_FetchOrders.fetchAndUpdateOrders_Execute(EbayJob_FetchOrders.java:64)
at com.test.jobs.EbayJob_FetchOrders.execute(EbayJob_FetchOrders.java:48)
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)

相关的弹簧配置:

<bean id="hibernateSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- annotation.AnnotationSessionFactoryBean -->
    <property name="dataSource" ref="mysqlDataSource" />
......

没有hibernate.current_session_context_class

<tx:annotation-driven  transaction-manager="transactionManager" /> 
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="hibernateSessionFactory"/>
</bean>

<tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRES_NEW" rollback-for="Throwable"/>
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:advisor pointcut="execution(* com.test.jobs.*.*Job*.*_Execute(..))" advice-ref="transactionInterceptor"/>
</aop:config>

如您所见,该方法被包裹在代理中,仍然没有找到会话!该方法由调度程序的作业运行,而不是控制器方法,它从 spring 上下文中获取 daos 等。事务等在控制器方法上运行良好,我正在使用@transactional。

版本:hibernate-4.3.5、spring-4.0.4

任何线索表示赞赏。

4

1 回答 1

0

如果您要将事务管理器配置为注释驱动,则不需要下面的代码

<tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRES_NEW" rollback-for="Throwable"/>
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:advisor pointcut="execution(* com.test.jobs.*.*Job*.*_Execute(..))" advice-ref="transactionInterceptor"/>
</aop:config>

只是 @Transactional(propagation= Propagation.REQUIRES_NEW) 将处理它

<tx:annotation-driven  transaction-manager="transactionManager" /> 
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="hibernateSessionFactory"/>
</bean>

但是我认为您只能在功能级别将 (propagation= Propagation.REQUIRES_NEW) 设置为

@Service
public class SomeService{

@Autowired
private DataSource dataSource;

@Transactional(propagation=Propagation.REQUIRES_NEW)
public void useDataSource() {
//save record
}

}
于 2014-09-12T12:48:53.813 回答