Spring H2 DB 的启动两个数据源配置无法创建表和插入数据
Spring Boot Two Datasource configuration for H2 DB failed to create tables and insert data
我正在寻找在我的 Spring 引导应用程序中配置两个 DS,并遵循 link - https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7。我们已经创建了 application.yml
个文件。
spring:
datasource:
jdbcUrl: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
driverClassName: org.h2.Driver
username: sa
password:
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true
h2:
console:
enabled: true
path: /h2-console
my:
datasource:
jdbcUrl: jdbc:h2:mem:test2db;DB_CLOSE_DELAY=-1
driverClassName: org.h2.Driver
username: sa
password:
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true
h2:
console:
enabled: true
path: /h2-console
这是代码
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "localKEntityManagerFactory",
transactionManagerRef = "localKTransactionManager",
basePackages = "com.mastercard.merchantbinding.repository.K")
@EntityScan(basePackages = ENTITY_PACKAGE)
public class LocalKH2DataSourceConfig {
@Bean(name = "localKdataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "localKEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("localKdataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages(ENTITY_PACKAGE).persistenceUnit("LOCAL_K").build();
}
@Bean(name = "localKTransactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("localKEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
另一个
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "localSEntityManagerFactory",
transactionManagerRef = "localSTransactionManager",
basePackages = "com.mastercard.merchantbinding.repository.S")
@EntityScan(basePackages = ENTITY_PACKAGE)
public class LocalSH2DataSourceConfig {
@Primary
@Bean(name = "localSdataSource")
@ConfigurationProperties(prefix = "my.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "localSEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("localSdataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages(ENTITY_PACKAGE).persistenceUnit("LOCAL_S").build();
}
@Primary
@Bean(name = "localSTransactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("localSEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
错误-
Table TRAN_DATA not found; SQL statement: --insert into ABC ------- [42S02-60]
有什么指点吗?
您的配置是正确的,但是 auto-ddl 不工作的原因与您在创建数据源期间手动创建 LocalContainerEntityManagerFactoryBean
有关。
您可以通过指示每个 bean 手动执行 DDL 操作来缓解这种情况,例如:
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, Qualifier("dataSource") DataSource dataSource) {
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "update");
return builder.dataSource(dataSource)
.packages("com.foo.springtest.users.models")
.persistenceUnit("user")
.properties(properties)
.build();
}
请注意以下两行指示休眠到 运行 DDL:
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "update");
我正在寻找在我的 Spring 引导应用程序中配置两个 DS,并遵循 link - https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7。我们已经创建了 application.yml
个文件。
spring:
datasource:
jdbcUrl: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
driverClassName: org.h2.Driver
username: sa
password:
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true
h2:
console:
enabled: true
path: /h2-console
my:
datasource:
jdbcUrl: jdbc:h2:mem:test2db;DB_CLOSE_DELAY=-1
driverClassName: org.h2.Driver
username: sa
password:
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true
h2:
console:
enabled: true
path: /h2-console
这是代码
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "localKEntityManagerFactory",
transactionManagerRef = "localKTransactionManager",
basePackages = "com.mastercard.merchantbinding.repository.K")
@EntityScan(basePackages = ENTITY_PACKAGE)
public class LocalKH2DataSourceConfig {
@Bean(name = "localKdataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "localKEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("localKdataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages(ENTITY_PACKAGE).persistenceUnit("LOCAL_K").build();
}
@Bean(name = "localKTransactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("localKEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
另一个
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "localSEntityManagerFactory",
transactionManagerRef = "localSTransactionManager",
basePackages = "com.mastercard.merchantbinding.repository.S")
@EntityScan(basePackages = ENTITY_PACKAGE)
public class LocalSH2DataSourceConfig {
@Primary
@Bean(name = "localSdataSource")
@ConfigurationProperties(prefix = "my.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "localSEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("localSdataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages(ENTITY_PACKAGE).persistenceUnit("LOCAL_S").build();
}
@Primary
@Bean(name = "localSTransactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("localSEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
错误-
Table TRAN_DATA not found; SQL statement: --insert into ABC ------- [42S02-60]
有什么指点吗?
您的配置是正确的,但是 auto-ddl 不工作的原因与您在创建数据源期间手动创建 LocalContainerEntityManagerFactoryBean
有关。
您可以通过指示每个 bean 手动执行 DDL 操作来缓解这种情况,例如:
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, Qualifier("dataSource") DataSource dataSource) {
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "update");
return builder.dataSource(dataSource)
.packages("com.foo.springtest.users.models")
.persistenceUnit("user")
.properties(properties)
.build();
}
请注意以下两行指示休眠到 运行 DDL:
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "update");