spring批处理导致spring数据不提交事务

时间:2014-12-04 07:35:55

标签: spring spring-mvc spring-data

我有一个spring mvc应用程序,其批处理过程由spring批处理。如果我删除批处理配置,则提交所有事务。如果运行批处理作业,批处理作业将成功完成,但不会将数据提交到数据库 我的配置如下

@Configuration
@EnableWebMvc
@EnableAsync
@EnableScheduling
@EnableBatchProcessing(modular = false)
@EnableTransactionManagement
@EnableRabbit
@EnableJpaRepositories(basePackages = "zw.co.econet.workstation.repositories")
@ComponentScan(basePackages = {"zw.co.workstation"})
@PropertySource(value = {"classpath:application.properties"})
public class WebConfiguration extends WebMvcConfigurerAdapter {
    @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public InternalResourceViewResolver jspViewResolver() {
    InternalResourceViewResolver bean = new InternalResourceViewResolver();
    bean.setPrefix("/WEB-INF/pages/");
    bean.setSuffix(".jsp");
    return bean;
    }
}

Spring批量配置:

@Configuration
public class BatchConfiguration {
@Autowired
private JobBuilderFactory jobs;

@Autowired
private StepBuilderFactory steps;
@Autowired
DataSource dataSource;

@Qualifier("creditQueueItemWriter")
@Autowired

private ItemWriter queueItemWriter;
@Qualifier("creditQueueProcessor")
@Autowired
private CreditQueueProcessor creditQueueProcessor;
@Qualifier("creditQueueReader")
@Autowired
private CreditQueueReader creditQueueReader;
@Qualifier("transactionManager")
@Autowired
private PlatformTransactionManager transactionManager;


@Bean
public AsyncTaskExecutor taskExecutor() {
    return new SimpleAsyncTaskExecutor();
}

@Bean
@Autowired
protected Step creditSubscriberStep() throws Exception {
    return steps.get("creditSubscriberStep")
            .allowStartIfComplete(true)
            .startLimit(3)
            .chunk(10)
            .reader(creditQueueReader)
            .processor(creditQueueProcessor)
            .writer(queueItemWriter)
            .faultTolerant()
            .build();
}

@Bean
public Job creditSubscribersJob() throws Exception {
    JobBuilder builder = jobs.get("creditSubscriberJob");
    return builder
            .start(creditSubscriberStep())
            .build();
}

@Bean
public JobLauncher jobLauncher() throws Exception {
    SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
    jobLauncher.setJobRepository(jobRepository());
    jobLauncher.setTaskExecutor(taskExecutor());
    jobLauncher.afterPropertiesSet();
    return jobLauncher;
}


@Bean
public JobRepository jobRepository() {
    try {
        JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTransactionManager(transactionManager);
        factoryBean.setIsolationLevelForCreate("DEFAULT");
        return factoryBean.getObject();
    } catch (Exception e) {
        return null;
    }
}


@Bean
public DataSourceInitializer databasePopulator() {
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(new ClassPathResource("org/springframework/batch/core/schema-mysql.sql"));
    populator.setContinueOnError(true);
    populator.setIgnoreFailedDrops(true);
    DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDatabasePopulator(populator);
    initializer.setDataSource(dataSource);
    return initializer;
}

}

信用作者:     @服务     公共类CreditQueueItemWriter实现ItemWriter {

private Logger logger = LoggerFactory.getLogger(getClass());
@Qualifier("creditQueueService")
@Autowired
private CreditQueueService creditQueueService;


@Override
public void write(List<? extends CreditQueue> list) throws Exception {
    logger.info("Processing credit list with size {}", list.size());
    for (CreditQueue creditQueue : list) {
        logger.info("Updating >>>> {} ", creditQueue);
        creditQueue.setProcessingState("PROCESSED");
        creditQueueService.save(creditQueue);

    }
    logger.info("chunk processed");
    }
}

0 个答案:

没有答案