spring boot batch多线程步骤数据库连接被关闭错误

时间:2016-08-04 16:20:04

标签: spring-boot spring-batch

我正在使用带弹簧批次的弹簧靴。我正在实现一个并行流程“多线程步骤”http://docs.spring.io/spring-batch/reference/html/scalability.html

我配置了两个数据源,一个用于读取,另一个用于写入。这是数据源类。

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "db1EntityManagerFactory",
transactionManagerRef="db1TransactionManager",
basePackages ="com.domain1.data.repositories")
public class DataSource1{

    @Primary
    @Bean(name="db1Datasource")
    @ConfigurationProperties(prefix="spring.db1Datasource")
    public DataSource db1Datasource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name="db1EntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean db1EntityManagerFactory(
            EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(db1Datasource())
                .packages("com.domain1.data.model")
                .persistenceUnit("db1")
                .build();
    }

    @Primary
    @Bean(name = "db1TransactionManager")
    public PlatformTransactionManager db1TransactionManager(
            @Qualifier("db1EntityManagerFactory") EntityManagerFactory db1EntityManagerFactory) {
        return new JpaTransactionManager(db1EntityManagerFactory);
    }
}

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "db2EntityManagerFactory",
transactionManagerRef="db2TransactionManager",
basePackages ="com.domain2.data.repositories")
public class DataSource2{

    @Bean(name="db2Datasource")
    @ConfigurationProperties(prefix="spring.db2Datasource")
    public DataSource db2Datasource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name="db2EntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean db2EntityManagerFactory(
            EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(db2Datasource())
                .packages("com.domain2.data.model")
                .persistenceUnit("db2")
                .build();
    }

    @Bean(name = "db2TransactionManager")
    public PlatformTransactionManager db2TransactionManager(
            @Qualifier("db2EntityManagerFactory") EntityManagerFactory db2EntityManagerFactory) {
        return new JpaTransactionManager(db2EntityManagerFactory);
    }
}



@Configuration
@EnableBatchProcessing
public class BatchConfig {
       private static final Logger LOG = LoggerFactory.getLogger(BatchConfig.class);

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    @Qualifier("db1Datasource")
    private DataSource bd1Datasource;

    @Autowired
    @Qualifier("db2EntityManagerFactory")
    private LocalContainerEntityManagerFactoryBean db2EntityManagerFactory;

    @Autowired
    @Qualifier("db2TransactionManager")
    private PlatformTransactionManager db2TransactionManager;

    @Bean(destroyMethod="")
    @StepScope
    public JdbcCursorItemReader<table1> table1Reader() throws Exception {
        String sql = "SELECT * FROM table1;
        JdbcCursorItemReader<table1> reader = new JdbcCursorItemReader<table1>();
        reader.setSql(sql);
        reader.setDataSource(db1Datasource);
        reader.setRowMapper(new resultSetMapper());
        reader.afterPropertiesSet();
        reader.setSaveState(true);
        return reader;
    }

    @Bean
    public dbProcessor dbProcessor() {
        return new dbProcessor ();
    }

    @Bean
    public ItemWriter<table2> table2Writer() {
        JpaItemWriter<table2> writer = new JpaItemWriter<table2>();
        writer.setEntityManagerFactory(table2EntityManagerFactory.getObject());
        return writer;
    }

     @Bean(name="batchJob")
     public Job batchJobJob() throws Exception {
           return jobBuilderFactory.get("batchJobJob")
                      .incrementer(new RunIdIncrementer())
                      .listener(new JobCompletionNotificationListener())
                      .flow(batchStep())
                      .end()
                      .build();
     }

     @Bean
     public Step batchStep() throws Exception {
         return stepBuilderFactory.get("batchStep")
                 .transactionManager(batchTransactionManager)
                 .<table1, table2> chunk(100)
                 .reader(table1Reader())
                 .processor(Processor())
                 .writer(table2Writer())
                 .faultTolerant()
                 .taskExecutor(taskExecutor())
                 .build();
     }

     @Bean
     public TaskExecutor taskExecutor(){


SimpleAsyncTaskExecutor asyncTaskExecutor=new SimpleAsyncTaskExecutor("batch_task");
                 asyncTaskExecutor.setConcurrencyLimit(5);
         return asyncTaskExecutor;
     }

}

我是com.microsoft.sqlserver.jdbc.SQLServerException:连接已关闭错误。

如果我不使用taskExecutor bean,它会罚款。在这种情况下,我错过了什么?你能帮我做些什么吗?还有哪种方法可以并行处理?多线程步骤或分区?

由于

0 个答案:

没有答案