gpt4 book ai didi

不同模式下的 Spring Batch 元数据表

转载 作者:行者123 更新时间:2023-12-02 09:04:24 25 4
gpt4 key购买 nike

我的应用程序中有一个连接到 Oracle 数据库的数据源。是否可以通过此数据源访问包含 Spring-batch 元数据表的另一个架构?该数据源的用户拥有访问其他架构的所有权限。

我已经尝试过 JobRepository 的“tablePrefix”属性,例如“Schema.batch_”。但这不起作用。简而言之,我寻找告诉 Spring-batch 访问元数据表的方法,例如“select ....from Schema.batch_..”而不是“select ...from batch_...”。

最佳答案

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

tablePrefix 对我来说也不起作用(我尝试了不同的情况 - 没有一个能解决问题)。

所以最后我决定为 Spring Batch 配置一个单独的数据源,指向 batch 模式。这是我的做法。

application.properties文件中,我有像spring.datasource.*这样的标准 Prop ,它用作应用程序的主要数据源。

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

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

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

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

...

}

这使得 Spring Batch 使用单独的数据源。

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

For Postgres 9.4 you can specify schema in the connection URL using currentSchema parameter: jdbc:postgresql://host:port/db?currentSchema=batch

For Postgres before 9.4 you can specify schema in the connection URL using searchpath parameter: jdbc:postgresql://host:port/db?searchpath=batch

Or you can create a separate postgres user/role for batch schema and setup search_path for that user: ALTER USER BATCH_DB_USER SET search_path to 'batch';

In Oracle each user has their own schema (as far as i know) and no way to set schema in the connection URL like for postgres (I may be wrong): jdbc:oracle:thin:@//host:port/sid

So you need to create a separate user for batch schema in Oracle. Another way is to use spring.batch.datasource.validation-query=ALTER SESSION SET CURRENT_SCHEMA=batch (I didn't try this)

因此,Spring Batch 通过这种方式使用配置为使用专用 batch 架构的单独数据源。批量查询看起来仍然像 select ...from batch_... 但它针对 batch 架构运行。应用程序正在使用指向应用程序专用架构app的常规数据源。

此解决方案已使用 Spring Boot v1.2.5.RELEASE 和 Postgres 9.4.1 进行测试

希望这有帮助。

关于不同模式下的 Spring Batch 元数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30994838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com