不同模式中的Spring-Batch元数据表 [英] Spring-batch metadata tables in different schema
问题描述
我的应用程序中有一个连接到Oracle数据库的数据源。是否可以通过此数据源访问包含Spring-Batch元数据表的另一个模式?此数据源的用户有权访问其他架构。
我已经尝试过JobRepository的"tablePrefix"属性,例如"Schema.Batch_"。但它不起作用。简而言之,我搜索告诉Spring-Batch访问类似于"SELECT.FROM Schema.Batch_.."的元数据表的方法。不是"SELECT.FROM BATCH_."。
推荐答案
我遇到了同样的问题,因为我希望将应用程序表保留在一个架构中,而将批次表保留在单独的架构中(使用postgres)。
tablePrefix
对我也不起作用(我尝试了不同的情况-都不能解决问题)。
所以最后我决定为Spring Batch配置一个指向batch
模式的单独数据源。我是这样做的。
在applation.properties文件中,我有类似spring.datasource.*
这样的标准道具,它作为主数据源用于应用程序。
和像spring.batch.datasource.*
这样的道具是非标准的,是仅在下面提供的代码中使用的辅助数据源。
以下是应用程序.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.*
:
对于Postgres 9.4,您可以使用
currentSchema
参数在连接URL中指定模式:jdbc:postgresql://host:port/db?currentSchema=batch
对于Postgres 9.4之前的版本,您可以使用
您也可以为searchpath
参数在连接URL中指定模式:jdbc:postgresql://host:port/db?searchpath=batch
batch
架构和设置search_path
创建单独的postgres用户/角色:ALTER USER BATCH_DB_USER SET search_path to 'batch';
在Oracle中,每个用户都有自己的模式(据我所知),无法像Postgres那样在连接URL中设置模式(我可能是错的):
因此您需要为Oracle中的jdbc:oracle:thin:@//host:port/sid
batch
架构创建单独的用户。另一种方法是使用spring.batch.datasource.validation-query=ALTER SESSION SET CURRENT_SCHEMA=batch
(我没有尝试这个)
因此,Spring Batch以这种方式使用配置为使用专用batch
模式的单独数据源。批处理查询看起来仍然类似select ...from batch_...
,但是它针对batch
架构运行。并且应用程序正在使用指向应用程序专用架构的常规数据源app
。
此解决方案使用Spring Boot v1.2.5.RELEASE和Postgres 9.4.1进行了测试
希望这对您有帮助。
这篇关于不同模式中的Spring-Batch元数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!