【问题标题】:Cannot create my Spring Batch tables in a different schema in Postgres无法在 Postgres 的不同模式中创建我的 Spring Batch 表
【发布时间】:2020-11-26 03:51:40
【问题描述】:

我有一个使用 Hibernate 和 Spring Batch 的 Spring Boot 应用程序。我正在为我的后端数据库使用 PostgresSQL。

我的项目配置了 2 个不同的数据源。 一个用于 Hibernate,一个用于 Spring Batch。它们都在同一个数据库中但在不同的架构中。

我的spring批处理连接字符串如下:

spring.batch.datasource.url=jdbc:postgresql://localhost:5432/mytestapp?currentSchema=springbatch 

我的数据源配置如下:

@Bean("b_ds_prop")
    @ConfigurationProperties("spring.batch.datasource")
    public DataSourceProperties dataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean("b_ds")
    @DependsOn({"b_ds_prop"})
    public DataSource batchFrameworkDatasource(DataSourceProperties dataSourceProperties) {
        System.out.println("start print");
//      dataSourceProperties.getSchema().stream().forEach(System.out::println);
//      System.out.println("datasrcprop is null : " + (dataSourceProperties==null));
//      System.out.println("datasrcprop.schema is null : " + (dataSourceProperties.getSchema()==null));
        System.out.println("end print");
//      dataSourceProperties.setSchema(Arrays.asList("spring_batch"));
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }
    
    @Bean
    @DependsOn({"b_ds"})
    public BatchConfigurer defaultBatchConfigurer(@Qualifier("b_ds") DataSource dataSource) {
        return new DefaultBatchConfigurer(dataSource);
    }

currentSchema=springbatch 设置不受尊重,无论我做什么。 我所有的 Spring Batch 表在 Postgres 中总是以 public 结尾。

名为springbatch 的架构已经存在于我的数据库中:mytestapp

我试过currentSchema=springbatch, public,我什至试过spring.batch.table-prefix=springbatch.

我已经尝试了所有方法,但我仍然无法理解为什么我的批处理表总是以 public 模式结束。

请注意,我的项目中有另一个连接字符串,例如:

spring.hib.datasource.url=jdbc:postgresql://localhost:5432/mytestapp

我还尝试以编程方式检查 schemaDataSourceProperties 中的值。

System.out.println("datasrcprop.schema is null : " + (dataSourceProperties.getSchema()==null));

这评估为真。

如何为 Spring Batch 设置不同的架构?

任何帮助都会很棒。

谢谢。

【问题讨论】:

  • Spring Batch 针对数据源工作(它不知道模式)并且不创建表。由于您创建了 BatchConfigurer 并将 b_ds 数据源传递给它,因此您需要在启动应用程序之前手动针对该数据源运行 postgres DDL 脚本,或者告诉 Spring Boot 为您执行此操作(并确保它将使用 Spring Batch 表初始化 那个 数据源)。

标签: java spring postgresql spring-batch datasource


【解决方案1】:

默认情况下,Spring boot 在启动时会自动创建 Spring Batch 元数据表。它执行位于org.springframework.batch.core 包中的默认脚本。在您的情况下,此脚本是 schema-postgresql.sql。此脚本在 postgre 默认模式中创建表,因此 public 如果未指定任何内容(并且由于我们不知道 Spring Boot 使用哪个数据源来创建 Spring Batch 元数据表,我们不知道默认模式是什么)。

在执行创建脚本时,spring boot 不考虑spring.batch.table-prefix 属性,该属性仅用于表示元数据表所在的spring batch。

如果你想控制这个 spring boot 功能,你应该:

  • 修改创建脚本(添加springbatch架构)并给spring boot修改脚本的路径(spring.batch.schema属性)

  • 在启动应用程序之前自行创建 Spring Batch 元数据表并禁用 Spring Boot 自动创建(spring.batch.initialize-schema=never 属性)

祝你好运

【讨论】:

  • 我不能在启动过程中只修改一次脚本,因为 Spring Batch 以后不知道如何定位它。
  • 如果你从spring批处理jar中提取sql脚本,然后修改它添加你的模式,然后将它保存在你项目的资源文件夹中,它会工作不是吗?请记住,Spring 批处理不需要脚本。只有 Spring Batch 的 Spring Boot 自动配置功能需要它。
【解决方案2】:

你试过这个配置吗:

spring.datasource.url=jdbc:postgresql://localhost:5432/mytestapp

#spring batch properties
spring.batch.job.enabled=false
spring.batch.initializer.enabled=false
spring.batch.initialize-schema=never
spring.batch.table-prefix=springbatch.

【讨论】:

  • 现在我没有桌子,因为你提到了spring.batch.initialize-schema=never。不工作。
猜你喜欢
  • 2020-04-20
  • 2015-09-08
  • 2018-12-19
  • 2020-07-20
  • 2016-10-29
  • 2016-11-06
  • 2016-01-16
  • 2021-03-12
  • 2019-04-06
相关资源
最近更新 更多