不同模式中的Spring-batch元数据表

Sof*_*ker 5 spring spring-batch

我有一个数据源连接到我的应用程序中的Oracle数据库.是否可以通过此数据源访问包含Spring批处理元数据表的另一个模式?此数据源的用户具有访问其他模式的所有权限.

我已经尝试过JobRepository的"tablePrefix"属性,例如"Schema.batch_".但它不起作用.简而言之,我搜索告诉Spring-batch访问元数据表的方法,例如"select .... from Schema.batch_ .."而不是"select ... from batch _...".

小智 10

我遇到了同样的问题,因为我想将应用程序表保存在一个模式中,并将批处理表保存在一个单独的表中(使用postgres).

tablePrefix 对我来说也没有用(我尝试了不同的案例 - 没有一个能解决问题).

所以最后我决定为Spring Batch配置一个单独的DataSource指向一个batch模式.我就是这样做的.

application.properties文件中,我有标准的道具spring.datasource.*,用于将应用程序用作主数据源.

而类似的道具spring.batch.datasource.*是非标准的,并且是仅在下面提供的代码中使用的辅助数据源.

以下是application.properties文件的示例:

spring.datasource.url=APP_DB_CONNECTION_URL
spring.datasource.username=APP_DB_USER
spring.datasource.password=APP_DB_PASS

spring.batch.datasource.url=BATCH_DB_CONNECTION_URL
spring.batch.datasource.username=BATCH_DB_USER
spring.batch.datasource.password=BATCH_DB_PASS
Run Code Online (Sandbox Code Playgroud)

然后在BatchConfiguration.java中,它是应用程序源的一部分,我添加了getBatchDataSource读取spring.batch.datasource.*属性的方法:

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

  @Bean
  @ConfigurationProperties(prefix="spring.batch.datasource")
  public DataSource getBatchDataSource(){
    return DataSourceBuilder.create().build();
  }

  ...

}
Run Code Online (Sandbox Code Playgroud)

这使Spring Batch使用单独的数据源.

现在重要的是spring.batch.datasource.*正确设置:

对于Postgres 9.4,您可以使用currentSchema参数在连接URL中指定模式:jdbc:postgresql://host:port/db?currentSchema=batch

对于9.4之前的Postgres,您可以使用searchpath参数在连接URL中指定模式:jdbc:postgresql://host:port/db?searchpath=batch

或者,您可以为该用户的batch架构和设置创建单独的postgres用户/角色search_path:ALTER USER BATCH_DB_USER SET search_path to 'batch';

在Oracle中,每个用户都有自己的架构(据我所知),没有办法在连接URL中设置架构,就像postgres一样(我可能错了): jdbc:oracle:thin:@//host:port/sid

因此,您需要为batchOracle中的架构创建单独的用户.另一种方法是使用spring.batch.datasource.validation-query=ALTER SESSION SET CURRENT_SCHEMA=batch(我没试过这个)

因此,Spring Bath使用单独的数据源配置为使用专用batch模式.批量查询仍然看起来像是select ...from batch_...针对batch模式运行.应用程序使用指向应用程序专用模式的常规数据源app.

使用Spring Boot v1.2.5.RELEASE和Postgres 9.4.1测试了该解决方案

希望这可以帮助.

  • 设置 spring.batch.tableprefix=<yourschema>.batch_ 应该可以解决问题 (2认同)