Spring使用定时器时无法获取事务问题

来源:互联网 发布:json2.js下载 编辑:程序博客网 时间:2024/06/05 22:51

本人Spring菜鸟,当我使用Springcloud+redis队列时,我在其中一个模块启动定时器定时从Redis中取数据更新至数据库,出现了TransactionRequiredException,解决方ran案是在做更新操作的service层的方法上加上@Transactionl 并添加配置类如下即可完美解决事务问题:

import org.apache.tomcat.jdbc.pool.DataSource;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.orm.jpa.JpaTransactionManager;import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.annotation.TransactionManagementConfigurer;import javax.persistence.EntityManagerFactory;/** * Created by JENSEN on 2017/11/15. */@Configurationpublic class DataSourceConfig implements TransactionManagementConfigurer {    @Value("${spring.profiles.active}")    private String profiles;    @Value("${spring.datasource.url}")    private String url;    @Value("${spring.datasource.username}")    private String username;    @Value("${spring.datasource.password}")    private String password;    @Value("${spring.datasource.driverClassName}")    private String driverClassName;    @Value("${spring.datasource.max-active}")    private Integer maxActive;    @Value("${spring.datasource.max-idle}")    private Integer maxIdle;    @Value("${spring.datasource.min-idle}")    private Integer minIdle;    @Value("${spring.datasource.initial-size}")    private Integer initialSize;    @Value("${spring.datasource.validation-query}")    private String validationQuery;    @Value("${spring.datasource.test-on-borrow}")    private Boolean testOnBorrow;    @Value("${spring.datasource.test-on-return}")    private Boolean testOnReturn;    @Value("${spring.datasource.test-while-idle}")    private Boolean testWhileIdle;    @Bean    public DataSource dataSource() {        DataSource ds = new DataSource();        ds.setUrl(url);        ds.setUsername(username);        ds.setPassword(password);        ds.setDriverClassName(driverClassName);        ds.setMaxActive(maxActive);        ds.setMinIdle(minIdle);        ds.setInitialSize(initialSize);        ds.setValidationQuery(validationQuery);        ds.setTestOnBorrow(testOnBorrow);        ds.setTestOnReturn(testOnReturn);        ds.setTestWhileIdle(true);        return ds;    }    @Primary    @Bean    EntityManagerFactory entityManagerFactory() {        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();        emf.setDataSource(dataSource());        emf.setJpaVendorAdapter(new HibernateJpaVendorAdapter());        emf.setPackagesToScan("com.spring.jensen");        emf.setPersistenceUnitName("default");        emf.afterPropertiesSet();        return emf.getObject();    }    @Bean    public PlatformTransactionManager transactionManager() {        return new JpaTransactionManager(entityManagerFactory());    }    @Override    public PlatformTransactionManager annotationDrivenTransactionManager() {        return null;    }}

原创粉丝点击