Java &引用;找不到当前线程的会话“;对于计划任务

Java &引用;找不到当前线程的会话“;对于计划任务,java,spring,hibernate,Java,Spring,Hibernate,我有一个任务,将一些数据写入数据库,它工作正常。但是,当a将其作为计划任务时,它会启动,但会抛出org.hibernate.HibernateException:在访问数据库时,找不到当前线程的会话 这是我的bean.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w

我有一个任务,将一些数据写入数据库,它工作正常。但是,当a将其作为计划任务时,它会启动,但会抛出
org.hibernate.HibernateException:在访问数据库时,找不到当前线程的会话

这是我的bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

    <bean id="slfInstaller" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="org.slf4j.bridge.SLF4JBridgeHandler.install"/>
    </bean> 

    <context:annotation-config/>

    <context:component-scan base-package="com.task"/>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <aop:aspectj-autoproxy proxy-target-class="true"/>
...
    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
        <property name="driverClassName" value="com.task.JDBCDriverProxy" />
        <property name="url" value="jdbc:task:db" />
        <property name="initialSize" value="10" />
        <property name="maxActive" value="100" />
        <property name="validationQuery" value="select 1" />
        <property name="testOnBorrow" value="true" />
        <property name="jdbcInterceptors" value="QueryTimeoutInterceptor(queryTimeout=60)" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan">
            <list>
                <value>com.task.model</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            </props>
        </property>
    </bean>
</beans>
任务是:

@Service
public class Job {

  @Inject
  private ConfigurationHolder configurationHolder;

  @Scheduled(cron = "0 0 11 * * *")
  public void run() {
    JobConfiguration configuration = configurationHolder.getConfiguration(JobConfiguration.class);
    ...
    User user = User.getById(userId);
    ...
  }
}

配置的注入工作正常。可能是什么问题?

您是否检查了此答案?您的计划任务未在事务上下文中运行。尝试将@Transactionall注释添加到您的运行方法是的,我在我的bean.xml中添加了@Transactionall注释,并尝试将User.getById设置为事务性的,但它没有效果,谢谢!它起作用了!)
@Service
public class Job {

  @Inject
  private ConfigurationHolder configurationHolder;

  @Scheduled(cron = "0 0 11 * * *")
  public void run() {
    JobConfiguration configuration = configurationHolder.getConfiguration(JobConfiguration.class);
    ...
    User user = User.getById(userId);
    ...
  }
}