Spring Batch Reader for distributed DB2 database

人盡茶涼 提交于 2019-12-12 18:26:05

问题


I am trying to write a job using Spring batch framework. Job needs to get data from a clustered db2 database, call some logic on each fetched record and then store transformed data in same db ( different table than from where it was read). I am trying to write step1 as below,

@Bean
    public Step step1(StepBuilderFactory stepBuilderFactory,
            ItemReader<RemittanceVO> reader, ItemWriter<RemittanceClaimVO> writer,
            ItemProcessor<RemittanceVO, RemittanceClaimVO> processor) {

        return stepBuilderFactory.get("step1")
                .<RemittanceVO, RemittanceClaimVO> chunk(100).reader(reader)
                .processor(processor).writer(writer).build();
    }

Currently, I face two challenges due to database being DB2 and being clustered,

1.

SQLs provided for meta data at - /org/springframework/batch/core/schema-db2.sql doesn't work for distributed DB2. It fails on command , constraint JOB_INST_UN unique (JOB_NAME, JOB_KEY) .

Queries written in this file can be tweaked to distributed db2 or I can create tables manually too but I am not sure if I should create tables manually? if that will have some further complications?

I need all these tables because I wanted to used Spring batch for its PAUSE , RESTART functionalities.

2.

We need to fire all SELECT queries on DB2 with READ ONLY WITH UR SO question. If we don't run queries with this keyword, db can get locked.

Problem in point # 2 is that I can't use in built reader classes of Spring Batch (JdbcPagingItemReader etc )as those doesn't support this db2 specific keyword.

By reading useless simple examples on Internet that explain advantages of this framework, I thought that I will be up and running in a very short period but it looks I have to write own query provider classes, research meta data sqls and what not if db happens to be DB2 and distributed.

Has anybody implemented similar job for distributed Db2 database and guide me on above points?


回答1:


I guess, to solve point # 1 , I will create tables manually since I have confirmed in another question that tables will not get dropped automatically so recreation will not be needed. One time manual activity should be enough.

and I will solve point # 2 by specifying isolation levels at transaction level so WITH UR in SELECT queries will not be needed,

@Autowired
    private DataSource dataSource;
    @Bean
        public TransactionTemplate transactionTemplateUR(){
            TransactionTemplate txnTemplate = new TransactionTemplate();
            txnTemplate.setIsolationLevelName("ISOLATION_READ_UNCOMMITTED");
            txnTemplate.setTransactionManager(txnManager);
            return txnTemplate;
        }

        @Bean
        public PlatformTransactionManager txnManager(DataSource dataSource){
            DataSourceTransactionManager txnManager = new DataSourceTransactionManager();
            txnManager.setDataSource(dataSource);
            return txnManager;
        }


来源:https://stackoverflow.com/questions/39072510/spring-batch-reader-for-distributed-db2-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!