我的Spring启动应用程序中有两个H2
数据库。这些数据库中的每一个都负责不同实体的持久性。
第一个由Flyway
初始化,工作正常,但现在我需要初始化其他H2数据库。我想我发现用Flyway初始化多个数据库是不可能的。所以我想手动导入我的schema.sql
和data.sql
文件
这就是我现在所拥有的: 两个单独的DatasourceConfig文件:
第一个数据源配置(注意该数据源如何配置为主数据):
@Configuration
@Profile({"prod","test"})
@EnableJpaRepositories(
basePackages = "tracker.repository.h2",
entityManagerFactoryRef = "h2EntityManager",
transactionManagerRef = "h2TransactionManager")
public class PrivateH2DatasourceConfig {
@Autowired
private Environment env;
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean h2EntityManager() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(h2DataSource());
em.setPackagesToScan(
new String[]{"tracker.domain.h2"});
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto",
env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect",
env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Bean
@Primary
public DataSource h2DataSource() {
DriverManagerDataSource dataSource
= new DriverManagerDataSource();
dataSource.setDriverClassName(
env.getProperty("h2.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("h2.datasource.url"));
dataSource.setUsername(env.getProperty("h2.datasource.data-username"));
dataSource.setPassword(env.getProperty("h2.datasource.data-password"));
return dataSource;
}
@Bean
@Primary
public PlatformTransactionManager h2TransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(h2EntityManager().getObject());
return transactionManager;
}
}
第二个H2数据源配置文件:
@Configuration
@Profile("test")
@EnableJpaRepositories(
basePackages = "tracker.repository.wmx",
entityManagerFactoryRef = "wmxH2EntityManager",
transactionManagerRef = "wmxH2TransactionManager")
public class WmxH2DatasourceConfig {
@Autowired
private Environment env;
@Bean
public LocalContainerEntityManagerFactoryBean wmxH2EntityManager() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(wmxH2DataSource());
em.setPackagesToScan(
new String[]{"tracker.domain.wmx"});
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto",
env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect",
env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Bean
public DataSource wmxH2DataSource() {
DriverManagerDataSource dataSource
= new DriverManagerDataSource();
dataSource.setDriverClassName(
env.getProperty("test.h2.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("test.h2.datasource.url"));
dataSource.setUsername(env.getProperty("test.h2.datasource.data-username"));
dataSource.setPassword(env.getProperty("test.h2.datasource.data-password"));
return dataSource;
}
@Bean
public PlatformTransactionManager wmxH2TransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(wmxH2EntityManager().getObject());
return transactionManager;
}
}
然后我有application.properties:
#can be over ridden by run arguments
spring.profiles.active=test
flyway.baseline-on-migrate=true
## H2
h2.datasource.driver-class-name=org.h2.Driver
h2.datasource.url=jdbc:h2:./data/deliverytracker.h2
h2.datasource.data-username=x
h2.datasource.data-password=xxx
## H2 WMX (Test environment)
test.h2.datasource.driver-class-name=org.h2.Driver
test.h2.datasource.url=jdbc:h2:./data/wmx_test.h2
test.h2.datasource.data-username=x
test.h2.datasource.data-password=xxx
test.h2.datasource.initialize=true
test.h2.datasource.data=classpath:schema-test.sql,classpath:data-test.sql
最后一行是我如何尝试在正确的数据库中导入sql脚本,但它似乎没有做任何事情。创建了第一个(非主要)数据库文件,但它是空的。