transaction isolation level (READ UNCOMMITTED) of Spring Batch

1.5k views Asked by At

I want to set transaction isolation level to READ UNCOMMITTED, but is doesn't work.

Here is my job source.

TestJobConfiguration.java

@Slf4j
@Configuration
public class TestJobConfiguration {
    @Autowired
    private JobBuilderFactory jobBuilders;

    @Autowired
    private CustomJobExecutionListener customJobExecutionListener;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private StepBuilderFactory stepBuilders;

    @Bean(name = "testJob")
    public Job job() {
        JobBuilder jobBuilder = jobBuilders.get("testJob").listener(customJobExecutionListener);
        Step step = stepBuilders.get("testStep").tasklet(count()).transactionAttribute(transactionAttr())
        .build();

        return jobBuilder.start(step).build();
    }

    public TransactionAttribute transactionAttr() {
        RuleBasedTransactionAttribute tr = new RuleBasedTransactionAttribute();
        tr.setIsolationLevel(TransactionDefinition.ISOLATION_READ_UNCOMMITTED);
        return tr;
    }

    public Tasklet count() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext context) {
                StringBuilder countSql = new StringBuilder();
                countSql.append(" SELECT COUNT(id)");
                countSql.append(" FROM user");
                log.debug("test start");
                int count = jdbcTemplate.queryForObject(countSql.toString(), Integer.class);
                contribution.incrementWriteCount(count);
                log.debug("test end");
                log.debug("count : {}", count);
                return RepeatStatus.FINISHED;
            }
        };
    }

}

I executed below sql statement in Microsoft SQL Server Management Studio, and executed TestJob.

begin tran
delete  from user

I expected to complete job, but it stopped at sql execution point. My log is below.

...
2017-08-29T12:21:23.555+09:00 DEBUG   --- [           main] c.p.l.b.rank.job.TestJobConfiguration    : test start

When I change my sql statement, countSql.append(" SELECT COUNT(id)"); to countSql.append(" SELECT COUNT(id) WITH (READUNCOMMITTED)"); it works.

...
2017-08-29T13:44:43.692+09:00 DEBUG   --- [           main] c.p.l.b.rank.job.TestJobConfiguration    : test start
2017-08-29T13:44:43.726+09:00 DEBUG   --- [           main] c.p.l.b.rank.job.TestJobConfiguration    : test end
2017-08-29T13:44:43.726+09:00 DEBUG   --- [           main] c.p.l.b.rank.job.TestJobConfiguration    : count : 15178
2017-08-29T13:44:43.747+09:00  INFO   --- [           main] c.p.l.b.l.CustomJobExecutionListener     : 
<!-----------------------------------------------------------------
Protocol for testJob 
  Started     : Tue Aug 29 13:44:43 KST 2017
  Finished    : Tue Aug 29 13:44:43 KST 2017
  Exit-Code   : COMPLETED
  Exit-Descr. : 
  Status      : COMPLETED
Job-Parameter: 
  date=2017-08-29 13:44:43 +0900
JOB process time  : 0sec
----------------------------------------------------------------->

Why doesn't work isolation level of transaction attribute??

0

There are 0 answers