Spring Batch的事务隔离级别(READ UNCOMMITTED)

时间:2017-08-29 04:47:06

标签: sql-server transactions spring-batch transaction-isolation

我想将事务隔离级别设置为READ UNCOMMITTED,但是不起作用。

这是我的工作来源。

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;
            }
        };
    }

}

我在Microsoft SQL Server Management Studio中的sql语句下执行,并执行了TestJob。

begin tran
delete  from user

我希望完成工作,但它在sql执行点停止了。 我的日志在下面。

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

当我更改我的sql语句时,countSql.append(" SELECT COUNT(id)"); to countSql.append(" SELECT COUNT(id) WITH (READUNCOMMITTED)");它可以正常工作。

...
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
----------------------------------------------------------------->

为什么不能处理事务属性的隔离级别?

0 个答案:

没有答案