java - Spring 批处理作业存储库表未在我配置的架构中创建

标签 java spring spring-boot spring-batch

我也遇到了类似的问题。我有两个数据源,一个是主数据源,另一个是 Spring Batch 应该使用的数据源。 Spring Batch 正在主数据源中创建架构,并尝试在第二个数据源中插入/更新,这是我的代码:

异常:java.sql.SQLSyntaxErrorException:用户缺少权限或未找到对象:BATCH_JOB_INSTANCE in statements [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ?和 JOB_KEY = ?]

public class DataSourceConfig {
@Bean
@Primary
@ConfigurationProperties("spring.datasource")
public DataSourceProperties primaryDataSourceProperties() {
    return new DataSourceProperties();
}

@Bean("secondDataSourceProperties")
@ConfigurationProperties("spring.second-datasource")
public DataSourceProperties secondDataSourceProperties() {
    return new DataSourceProperties();
}

/**
 * Create primary (default) DataSource.
 */
@Bean
@Primary
public DataSource primaryDataSource(@Autowired DataSourceProperties props) {
    return props.initializeDataSourceBuilder().build();
}

/**
 * Create second DataSource and named "secondDatasource".
 */
@Bean("secondDatasource")
public DataSource secondDataSource(@Autowired 
@Qualifier("secondDataSourceProperties") DataSourceProperties props) {
    return props.initializeDataSourceBuilder().build();
}
}

还有

@Component
@Configuration
public class MyBatchConfigurer extends DefaultBatchConfigurer {

/**
 * Initialize the BatchConfigurer to use the datasource of your choosing
 *
 * @param secondDatasource
 */

@Autowired
@Qualifier("secondDatasource")
DataSource datasource;

@Override
protected JobRepository createJobRepository() throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDataSource(datasource);
    factory.setTransactionManager(transactionManager());
    factory.afterPropertiesSet();

    return factory.getObject();
}

private PlatformTransactionManager transactionManager() {
    return new DataSourceTransactionManager(datasource);
}
}

最佳答案

创建BatchConfigurer的自定义实现。

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.springframework.batch.core.configuration.BatchConfigurationException;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean;
import org.springframework.batch.core.launch.JobLauncher;
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.batch.core.repository.support.MapJobRepositoryFactoryBean;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;

@Component
public class MyBatchConfigurer implements BatchConfigurer {


    private PlatformTransactionManager transactionManager;
    private JobRepository jobRepository;
    private JobLauncher jobLauncher;
    private JobExplorer jobExplorer;
    @Autowired
    @Qualifier("secondDatasource")
    private DataSource dataSource;

    @Override
    public JobRepository getJobRepository() {
        return jobRepository;
    }

    @Override
    public PlatformTransactionManager getTransactionManager() {
        return new DataSourceTransactionManager(datasource);
    }

    @Override
    public JobLauncher getJobLauncher() {
        return jobLauncher;
    }

    @Override
    public JobExplorer getJobExplorer() {
        return jobExplorer;
    }

    @PostConstruct
    public void initialize() {
        try {
            this.jobRepository = createJobRepository();
            this.jobExplorer = createJobExplorer();
            this.jobLauncher = createJobLauncher();
        } catch (Exception e) {
            throw new BatchConfigurationException(e);
        }
    }

    protected JobLauncher createJobLauncher() throws Exception {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(jobRepository);
        jobLauncher.afterPropertiesSet();
        return jobLauncher;
    }

    protected JobRepository createJobRepository() throws Exception {
        JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
        factory.setDataSource(datasource);
        factory.setTransactionManager(transactionManager());
        factory.afterPropertiesSet();

        return factory.getObject();
    }

    protected JobExplorer createJobExplorer() throws Exception {
        JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
        jobExplorerFactoryBean.setDataSource(this.dataSource);
        jobExplorerFactoryBean.afterPropertiesSet();
        return jobExplorerFactoryBean.getObject();
    }
}

关于java - Spring 批处理作业存储库表未在我配置的架构中创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59398894/

相关文章:

java - 使用深度优先搜索创建迷宫

java - Spring Security - 在重定向到登录时保留 URL 参数

java - 如何限制使用 Spring-Boot 和 OAuth2 登录特定域

sql-server - 没有可用的 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' 类型的合格 bean

java - 如何在 Android 中将 TableRow 宽度设置为最大?

java - 栈和队列(读代码)

JAVA 运行时错误 : java. lang.UnsupportedClassVersionError。不支持的 major.minor 版本 51.0

java - 在特定场景中,在死信队列和 TTL 循环之后,如何使用 Spring 将 AMQP 消息发布到 parking 场队列?

java - Gradle - 更改 jar 的名称

http - Spring Boot Basic Auth 总是给出 401 错误