H2 DB的Spring Boot两个Datasource配置创建表和插入数据失败

Posted

技术标签:

【中文标题】H2 DB的Spring Boot两个Datasource配置创建表和插入数据失败【英文标题】:Spring Boot Two Datasource configuration for H2 DB failed to create tables and insert data 【发布时间】:2021-08-13 09:41:33 【问题描述】:

我希望在我的 Spring Boot 应用程序中配置两个 DS,并遵循链接 - 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]  

任何指针?

【问题讨论】:

【参考方案1】:

您的配置是正确的,但是 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();

请注意以下两行指示 hibernate 运行 DDL:

Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "update");

【讨论】:

以上是关于H2 DB的Spring Boot两个Datasource配置创建表和插入数据失败的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot - h2 DB 测试多次执行脚本

如何将 ArrayList 发布到 Spring Boot H2 DB

Spring Boot:无法登录 h2 控制台

在 Spring Boot 的同一个 pom.xml 中管理 H2 和 Postgres

为spring boot配置h2

Spring Boot 2.x 之 H2 数据库