"找不到当前线程的会话"用于计划任务

时间:2016-03-11 08:49:03

标签: java spring hibernate

我有一项任务是将一些数据写入db并且工作正常。但是当a使它成为一个计划任务时它会启动但在访问数据库时抛出org.hibernate.HibernateException: No Session found for current thread

这是我的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>

Spring配置:

@EnableScheduling
@Configuration
public class SpringConfiguration {

    @Inject
    private ApplicationContext context;

    @PostConstruct
    public void registerListeners() {
        SessionFactory sessionFactory = context.getBean(SessionFactory.class);
        EventListenerRegistry registry = ((SessionFactoryImplementor) sessionFactory).getServiceRegistry().getService(
                EventListenerRegistry.class);
        registry.getEventListenerGroup(EventType.POST_LOAD).appendListener(new PostLoadEventListener());
    }

    @Bean
    public Validator getValidator() {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        return factory.getValidator();
    }
}

任务:

@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);
    ...
  }
}

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

0 个答案:

没有答案