首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring :如何更改默认的隔离级别?

Spring :如何更改默认的隔离级别?
EN

Stack Overflow用户
提问于 2020-12-21 15:21:42
回答 1查看 2.4K关注 0票数 4

我在文件中读到:

“默认值为ISOLATION_SERIALIZABLE,它防止意外并发执行同一作业”

但是,当我同时启动不同的作业(默认隔离级别为SERIALIZABLE)时,我有一个错误:ORA-08177: can't serialize access for this transaction.是否正常?

其次,要将默认的隔离级别更改为READ_COMMITTED,我知道我们不能在application.properties中更改这个级别,并且必须重新定义BatchConfigurer。准确吗?

使用BasicBatchConfigurer,我必须定义一个显式构造器(默认构造函数未定义隐式超级构造函数BasicBatchConfigurer() )。但是,我有一个错误:

代码语言:javascript
复制
 Parameter 0 of constructor in MyBatchConfigurer required a bean of type 'org.springframework.boot.autoconfigure.batch.BatchProperties' that could not be found.

如何创建: BatchProperties属性、DataSource dataSource和TransactionManagerCustomizers transactionManagerCustomizers?

这是我的密码:

PeopleApplication.java

代码语言:javascript
复制
@SpringBootApplication
@EnableAutoConfiguration(exclude = { BatchAutoConfiguration.class })
public class PeopleApplication {
    
    
    public static void main(String[] args) throws Exception {
        
        ConfigurableApplicationContext ctx =  new SpringApplicationBuilder(PeopleApplication.class)
        .web(WebApplicationType.NONE) 
        .run(args);
        
        int exitValue = SpringApplication.exit(ctx);
        System.exit(exitValue);
    }
}

MyBatchConfigurer.java

代码语言:javascript
复制
@Component
@PropertySource("classpath:fileA.properties")
public class MyBatchConfigurer extends BasicBatchConfigurer implements CommandLineRunner, ExitCodeGenerator {

    protected MyBatchConfigurer(BatchProperties properties, DataSource dataSource, TransactionManagerCustomizers transactionManagerCustomizers) {
         super(properties, dataSource, transactionManagerCustomizers);
     }
     
     @Override
     protected String determineIsolationLevel() {
            return "ISOLATION_" + Isolation.READ_COMMITTED;
     }

    @Override
    public void run(String... args) {
        ...
    }

    ...
}

致以问候。

响应:

使用@EnableAutoConfiguration而不是:

代码语言:javascript
复制
@EnableAutoConfiguration(exclude = { BatchAutoConfiguration.class })

像这样,bean BatchProperties、DataSource和TransactionManagerCustomizers将被自动创建。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-22 03:41:22

请看马哈茂德的答复,其中解释得很清楚。

can't serialize access for this transaction in spring batch

示例的用法在下面,并且仅覆盖隔离级别。

代码语言:javascript
复制
--application.properties

spring.application.name=SpringBatch

####### SPRING ##############

spring.main.banner-mode=off
spring.main.web-application-type=none
spring.batch.initialize-schema=always
spring.batch.job.enabled=false // Disable default if you want to control

########JDBC Datasource########
#connection timeout 10 min
spring.datasource.hikari.connection-timeout=600000
spring.datasource.hikari.minimum-idle=5 
spring.datasource.hikari.maximum-pool-size=100
spring.datasource.hikari.idle-timeout=600000 
spring.datasource.hikari.max-lifetime=1800000 
spring.datasource.hikari.auto-commit=true 
spring.datasource.hikari.poolName=SpringBoot-HikariCP
spring.datasource.url=jdbc:oracle:thin:@ngecom.ae:1521:ngbilling
spring.datasource.username=ngbilling
springbatch.datasource.password=ngbilling

@SpringBootApplication
public class YourApplication {

    public static void main(String[] args) {
        SpringApplication.run(SsadapterApplication.class, args);
    }

}

// Your manual batch scheduler

 class BatchJobScheduler extends BasicBatchConfigurer {
        @Autowired
        private JobLauncher jobLauncher;
        @Autowired
        private ApplicationArguments args;
        @Autowired
        private Job myJob;
        protected BatchJobScheduler(BatchProperties properties, DataSource dataSource,
                TransactionManagerCustomizers transactionManagerCustomizers) {
            super(properties, dataSource, transactionManagerCustomizers);
        }
        @Override
        protected String determineIsolationLevel() {
            return "ISOLATION_" + Isolation.READ_COMMITTED;
        }
        //@Scheduled(cron = "${batch.cron}")
        public void notScheduledJob() {
                appId= args.getOptionValues("appId").get(0);
                JobParameters params = new JobParametersBuilder().addLong("jobId"+appId, System.currentTimeMillis())
                        .toJobParameters();
                jobLauncher.run(myJob, params);
        }
       

// Your batch Configuration And Spring will do everything for you to available

@Configuration
@EnableBatchProcessing
@EnableScheduling
public class BatchConfiguration {

    Logger logger = LoggerFactory.getLogger(BatchConfiguration.class);
    
  
    @Value("${batch.commit.chunk}")
    private Integer chunkSize;

    @Value("${batch.error.skipCount}")
    private Integer skipErrorCount;
    
    
    @Autowired
    private Environment environment;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private ApplicationArguments args;
    
    @Autowired
    private JdbcTemplate jdbcTemplate; 

    @Bean
    public Job myJob() throws Exception {
        return jobBuilderFactory.get("myJob").incrementer(new RunIdIncrementer())
                .listener(new JobListener()).start(myStep()).build();
    } 

   @Bean
    public Step myStep() throws Exception {
        return stepBuilderFactory.get("myStep").<InputObject, OutPutObject>chunk(chunkSize)
                .reader(yourReader(null)).processor(yourProcessor()).writer(yourWriter())
                //.faultTolerant()
                //.skipLimit(skipErrorCount).skip(Exception.class)//.noSkip(FileNotFoundException.class)
                .listener(invoiceSkipListener())
                //.noRetry(Exception.class)
                //.noRollback(Exception.class)
                .build();
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65395390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档