我遇到了同样的问题,因为我想将应用程序表保存在一个架构中,而将批处理表保存在一个单独的架构中(使用 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
然后在 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
对于 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 中,每个用户都有自己的架构(据我所知),并且无法像 postgres 那样在连接 URL 中设置架构(我可能错了):jdbc:oracle:thin:@//host:port/sid
因此您需要在 Oracle 中为 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 进行了测试
希望这会有所帮助。