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

Java 未在我配置的架构中创建Spring批处理作业存储库表,java,spring,spring-boot,spring-batch,Java,Spring,Spring Boot,Spring Batch,我也遇到了类似的问题。我有两个数据源,一个是主数据源,另一个是springbatch应该使用的数据源。spring batch正在主数据源中创建架构,并尝试在第二个数据源中插入/更新以下是我的代码: 异常:java.sql.SQLSyntaxErrorException:用户缺少权限或找不到对象:语句中的批处理作业实例[从批处理作业实例中选择作业实例ID、作业名称,其中作业名称=?和作业密钥=?] public class DataSourceConfig { @Bean @Primary @C

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

异常:java.sql.SQLSyntaxErrorException:用户缺少权限或找不到对象:语句中的批处理作业实例[从批处理作业实例中选择作业实例ID、作业名称,其中作业名称=?和作业密钥=?]

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


创建
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();
    }
}

创建
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();
    }
}

这就是我所做的,让它工作 我刚刚添加了一个bean,其中注入了第二个数据源

@Bean
BatchConfigurer configurer(@Autowired @Qualifier("secondDatasource") DataSource dataSource) {
    return new DefaultBatchConfigurer(dataSource);
}
但仍然没有创建模式 我用了:

@Configuration
@Profile({"dev","prod"})
public class JobRepositorySchemaConfig {
private final String JOB_REPO_SCHEMA = "classpath:batch_repo_schema.sql";

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

@Autowired
WebApplicationContext webApplicationContext;

@PostConstruct
public void loadIfInMemory() throws Exception {
    Resource resource = webApplicationContext.getResource("classpath:/org/springframework/batch/core/schema-drop-hsqldb.sql");
    Resource resource2 = webApplicationContext.getResource("classpath:/org/springframework/batch/core/schema-hsqldb.sql");
    ScriptUtils.executeSqlScript(datasource.getConnection(), resource);
    ScriptUtils.executeSqlScript(datasource.getConnection(), resource2);
}

}

我就是这样做的 我刚刚添加了一个bean,其中注入了第二个数据源

@Bean
BatchConfigurer configurer(@Autowired @Qualifier("secondDatasource") DataSource dataSource) {
    return new DefaultBatchConfigurer(dataSource);
}
但仍然没有创建模式 我用了:

@Configuration
@Profile({"dev","prod"})
public class JobRepositorySchemaConfig {
private final String JOB_REPO_SCHEMA = "classpath:batch_repo_schema.sql";

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

@Autowired
WebApplicationContext webApplicationContext;

@PostConstruct
public void loadIfInMemory() throws Exception {
    Resource resource = webApplicationContext.getResource("classpath:/org/springframework/batch/core/schema-drop-hsqldb.sql");
    Resource resource2 = webApplicationContext.getResource("classpath:/org/springframework/batch/core/schema-hsqldb.sql");
    ScriptUtils.executeSqlScript(datasource.getConnection(), resource);
    ScriptUtils.executeSqlScript(datasource.getConnection(), resource2);
}

}

欢迎来到StackOverflow!错误消息在我看来并不含糊。你们已经检查过用户权限和表的存在了吗?是的,我检查过了。未创建表
spring批处理正在主数据源中创建架构
这是不正确的。Spring批处理不创建表。可能是弹簧靴。因此,请确保在spring批处理配置中使用由spring Boot初始化的相同数据源。欢迎使用StackOverflow!错误消息在我看来并不含糊。你们已经检查过用户权限和表的存在了吗?是的,我检查过了。未创建表
spring批处理正在主数据源中创建架构
这是不正确的。Spring批处理不创建表。可能是弹簧靴。因此,请确保在spring批处理配置中使用由spring Boot初始化的相同数据源。