我有一个使用两个数据源的Spring Boot 2应用程序 - 一个是Oracle,一个是H2。H2数据源被设置为次要数据源,并且我希望在启动时为它创建模式,但它从未触发schema.sql文件。这是我的数据源配置文件:
@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文件:
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,因此从未尝试初始化模式。非常感谢您的帮助!
这是预期的行为。当指定了Primary数据源时,只会执行该数据源的模式和数据。为了实现这种行为,您需要为这两个数据源分别定义
DataSourceInitializer
。@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
来实现。