Spring Boot 2 多个数据源初始化模式
我有一个使用两个数据源的 Spring Boot 2 应用程序 - 一个 Oracle 和一个 H2.H2 数据源设置为辅助数据源,我想在启动时为其创建架构,但它永远不会触发 schema.sql 文件.这是我的数据源配置文件:
I have a Spring Boot 2 application that's using two datasources - one Oracle and one H2. The H2 datasource is set up as secondary, and I want to create the schema for it upon startup, but it never fires off the schema.sql file. This is my Data Source Config file:
@Configuration
public class DataSourceConfig {
@Bean
@Primary
@ConfigurationProperties("spring.datasource")
public DataSourceProperties primaryDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties("spring.datasource")
public DataSource primaryDataSource() {
return primaryDataSourceProperties().initializeDataSourceBuilder().build();
}
@Bean
@ConfigurationProperties("spring.runlogdatasource")
public DataSourceProperties runlogDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@ConfigurationProperties("spring.runlogdatasource")
public DataSource runlogDataSource() {
return runlogDataSourceProperties().initializeDataSourceBuilder().build();
}
}
这是我的 application.properties 文件:
This is my application.properties file:
spring.datasource.url=jdbc:oracle:thin:@my.database.com:1521/mydb
spring.datasource.username=test
spring.datasource.password=ENC(3PXcnoBndLpWN1EcMtmIn+odOwhdWjSrqANijutxuekKEIOco64Jew==)
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.initialization-mode=never
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maximum-pool-size=10
jasypt.encryptor.bean=stringEncryptor
spring.runlogdatasource.url=jdbc:h2:~/runlogdb;CIPHER=AES;DB_CLOSE_ON_EXIT=FALSE;
spring.runlogdatasource.username=sa
spring.runlogdatasource.password=ENC(3PXcnoBndLpWN1EcMtmIn+odOwhdWjSrqANijutxuekKEIOco64Jew==)
spring.runlogdatasource.driverClassName=org.h2.Driver
spring.runlogdatasource.platform=h2
spring.runlogdatasource.schema=classpath:schema-h2.sql
对于主数据源,我可以看到它进入了 DataSourceInitializerInvoker,这是它尝试加载架构的地方,但由于没有 schema-all.sql 文件,它会跳过该文件.但是,我确实有一个 schema-h2.sql 文件,但对于辅助数据源,它永远不会进入 DataSourceInitializerInvoker,因此永远不会尝试初始化模式.任何帮助将不胜感激!
For the primary datasource, I can see that it enters the DataSourceInitializerInvoker which is where it attempts to load up the schema, but since there are no schema-all.sql files it skips over that one. However, I do have a schema-h2.sql file, but for the secondary datasource it never enters the DataSourceInitializerInvoker and therefore never attempts to initialize the schema. Any help would be greatly appreciated!
推荐答案
这是预期行为.当指定主数据源时,仅执行此数据源的模式和数据.为了获得这种行为,您需要做的是为两个数据源中的每一个定义 DataSourceInitializer
.
This is expected behaviour. When Primary datasource is specified only the schema and data for this datasource is executed. What you need to do in order to have this behavior is to define DataSourceInitializer
for each of the two datasources.
@Bean
public DataSourceInitializer dataSourceInitializer1(@Qualifier("datasource1") DataSource datasource) {
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
resourceDatabasePopulator.addScript(new ClassPathResource("schema-h22.sql"));
resourceDatabasePopulator.addScript(new ClassPathResource("data-h22.sql"));
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(datasource);
dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
return dataSourceInitializer;
}
@Bean
public DataSourceInitializer dataSourceInitializer2(@Qualifier("datasource2") DataSource datasource) {
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
resourceDatabasePopulator.addScript(new ClassPathResource("schema-h21.sql"));
resourceDatabasePopulator.addScript(new ClassPathResource("data-h21.sql"));
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(datasource);
dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
return dataSourceInitializer;
}
您还需要禁用默认的 spring 数据初始化.您可以通过 spring.datasource.initialization-mode=never
You also need to disable the default spring data initialization. You can do this through spring.datasource.initialization-mode=never
相关文章