【问题标题】:Spring Batch - Problems Connecting to Postgres DatabaseSpring Batch - 连接到 Postgres 数据库的问题
【发布时间】:2020-12-13 12:18:27
【问题描述】:

到目前为止,我一直在使用带有 Spring Batch 的内存 H2 DB。但是,现在我切换到连接到外部 postgres 数据库。这是我的连接对象(有些混淆):

@Bean
public DataSource postgresDatasource() {
    DriverManagerDataSource datasource = new DriverManagerDataSource();
    datasource.setDriverClassName("org.postgresql.Driver");
    datasource.setUrl("jdbc:postgresql://x.x.x.x:xxxx/blah");
    datasource.setUsername("Joe");
    datasource.setPassword("password");
    return datasource;
}

当我启动我的应用程序时,我得到:

引起:org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback;错误的 SQL 语法 [SELECT JOB_INSTANCE_ID, JOB_NAME 来自 BATCH_JOB_INSTANCE,其中 JOB_NAME = ?和 JOB_KEY = ?]; 嵌套异常是 org.postgresql.util.PSQLException:错误:关系 “batch_job_instance”不存在

然后我读到 Spring Batch 使用数据库为其恢复/重试功能保存元数据,并且对于嵌入式数据库,这些是 Spring Batch 默认设置的表。好的,这就解释了为什么我以前从未见过这个错误。

但是,它说我可以设置这个属性:

spring.batch.initialize-schema=never

所以我把它放在我的 application.properties 文件中。但是,我仍然收到错误消息。如果有任何想法,我将不胜感激。

【问题讨论】:

标签: java postgresql spring-batch


【解决方案1】:

我能够自己解决这个问题。最终,我需要独立于我的实际目标关系数据库的 Spring Batch 存储库。所以我找到了这个参考:

https://github.com/spring-projects/spring-batch/blob/342d27bc1ed83312bdcd9c0cb30510f4c469e47d/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/DefaultBatchConfigurer.java#L84

我能够从该示例中获取 DefaultBatchConfigurer 类,并通过为嵌入式/本地数据源添加 @Qualifier 对数据源进行细微更改:

@Autowired(required = false)
public void setDataSource(@Qualifier("dataSource") DataSource dataSource) {
    this.dataSource = dataSource;
    this.transactionManager = new DataSourceTransactionManager(dataSource);
}

然后,在我的 Spring Batch 阅读器上(在我的另一个批处理配置类中),我通过为 postgres 数据源添加 @Qualifier 对数据源进行了微小的更改:

@Bean
public ItemReader<StuffDto> itemReader(@Qualifier("postgresDataSource")DataSource dataSource) {
return new JdbcCursorItemReaderBuilder<StuffDto>()
    .name("cursorItemReader")
    .dataSource(dataSource)
    .sql(GET_DATA)
    .rowMapper(new BeanPropertyRowMapper<>(StuffDto.class))
    .build();
}

然后最后(或者首先真的像我第一次做这些),我明确地命名了我的数据源 bean,以便 java 可以区分它们以按上述方式使用:

@Configuration
public class PersistenceContext {


    @Bean(name = "dataSource")
    public DataSource dataSource() {
        DriverManagerDataSource datasource = new DriverManagerDataSource();
        datasource.setDriverClassName("org.h2.Driver");
        datasource.setUrl("jdbc:h2:file:/tmp/test");
        datasource.setUsername("sa");
        datasource.setPassword("");
        return datasource;
    }

    @Bean(name = "postgresDataSource")
    public DataSource postgresDatasource() {
        DriverManagerDataSource datasource = new DriverManagerDataSource();
        datasource.setDriverClassName("org.postgresql.Driver");
        datasource.setUrl("jdbc:postgresql://x.x.x.x:xxxx/blah");
        datasource.setUsername("joe");
        datasource.setPassword("password");
        return datasource;    }
}

完成上述所有操作后,错误消失了,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 2022-08-18
    • 2023-03-23
    • 1970-01-01
    • 2020-03-02
    相关资源
    最近更新 更多