当前位置: 首页 > 工具软件 > Batch-CN > 使用案例 >

[解决异常] spring batch 报错 ORA-08177: 无法连续访问此事务处理

漆雕修能
2023-12-01

问题描述:

使用spring batch 连接oracle操作时,总是报出如下异常:

PreparedStatementCallback; SQL [INSERT into BATCH_JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION) values (?, ?, ?, ?)]; ORA-08177: 无法连续访问此事务处理

原因:spring batch 默认是 ISOLATION_SERIALIZABLE

官方文档说明:The default is ISOLATION_SERIALIZABLE, which prevents accidental concurrent execution of the same job (ISOLATION_REPEATABLE_READ would work as well).

解决办法:

1. 查阅资料:http://stackoverflow.com/questions/22364432/spring-batch-ora-08177-cant-serialize-access-for-this-transaction-when-running 都说将oracle事务级别从SERIALIZABLE调整为ISOLATION_READ_COMMITTED就行了。的确是这样。

2.自己根据资料实现:

package cn.yong.zheng.config;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * @author zhengyong
 *
 */
@Configuration
@EnableBatchProcessing
public class CommonBatchConfiguration{
	
	private static final Logger logger = LoggerFactory.getLogger(CommonBatchConfiguration.class);
    
    @Bean
    public JobRepositoryFactoryBean jobRepositoryFactoryBean(DataSource dataSource,PlatformTransactionManager transactionManager){
    	JobRepositoryFactoryBean jobRepositoryFactoryBean = new JobRepositoryFactoryBean();
    	jobRepositoryFactoryBean.setTransactionManager(transactionManager);
    	jobRepositoryFactoryBean.setDataSource(dataSource);
    	jobRepositoryFactoryBean.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED");
    	try {
    		jobRepositoryFactoryBean.afterPropertiesSet();
		} catch (Exception e) {
			logger.error("创建jobRepositoryFactoryBean异常:{}",e.getMessage());
		}
    	return  jobRepositoryFactoryBean;
    }
 
    @Bean
    public JobRepository jobRepository(JobRepositoryFactoryBean jobRepositoryFactoryBean){
    	JobRepository jobRepository = null;
		try {
			jobRepository = jobRepositoryFactoryBean.getObject();
		} catch (Exception e) {
			logger.error("创建jobRepository异常{}",e.getMessage());
		}
    	return jobRepository;
    }
    
    @Bean
    public SimpleJobLauncher  simpleJobLauncher(JobRepository jobRepository){
    	SimpleJobLauncher  simpleJobLauncher = new SimpleJobLauncher();
    	simpleJobLauncher.setJobRepository(jobRepository);
    	try {
			simpleJobLauncher.afterPropertiesSet();
		} catch (Exception e) {
			logger.error("创建simpleJobLauncher异常:{}",e.getMessage());
		}
		return simpleJobLauncher;
    }

}
使用自己创建的事务级别为 ISOLATION_READ_COMMITTED的launcher执行batch
package cn.yong.zheng.task;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 
 * @author zhengyong
 */
public class BlackListMemcacgeScheduledTask implements org.quartz.Job {

    private static final Logger logger = LoggerFactory.getLogger(BlackListMemcacgeScheduledTask.class);

    @Autowired
    private SimpleJobLauncher simpleJobLauncher;

    @Resource(name = "springBatchJob")
    private Job springBatchJob;

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        this.executeTask();
    }

    /**
     * 定时任务执行计划
     */
    private void executeTask() {
        try {
                JobExecution execution = simpleJobLauncher.run(springBatchJob,
                        new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters());
            } catch (Exception e) {
                logger.error("异常:{}", e.getMessage());
            }
    }

}


 类似资料: