当前位置: 首页 > 知识库问答 >
问题:

spring-boot应用程序不会将fixture加载到多个数据源中的一个

柯正谊
2023-03-14

在启动spring boot应用程序(1.5.2.Release)期间,我在加载数据夹具时遇到了一些问题。应用程序使用两个不同的数据库连接,一个连接到我们的客户postgresql数据库,我们没有权限在该数据库上创建、插入或更新任何内容。另一个数据库是本地嵌入式h2数据库(文件)。我希望在应用程序启动期间使用spring boot数据库初始化阶段将一些数据加载到h2数据库中,但数据从未插入到h2数据库中,它保持空状态,就像我使用squirrel-sql所看到的那样。

spring.datasource.abc.driver-class-name=org.postgresql.Driver
spring.datasource.abc.initialize=false
spring.datasource.abc.url=jdbc:postgresql://localhost:5432/abc
spring.datasource.abc.username=abc
spring.datasource.abc.password=abc

spring.datasource.def.driver-class-name=org.h2.Driver
spring.datasource.def.initialize=true
spring.datasource.def.url=jdbc:h2:./${path.prefix}def/def;DB_CLOSE_ON_EXIT=FALSE'
spring.datasource.def.data=classpath:/data-h2.sql

我的postgresql数据源的配置:

@Configuration
@EnableJpaRepositories(basePackages = "....abc", entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager")
public class AbcDatabaseConfig
{

    @Primary
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.abc")
    public DataSource dataSource()
    {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory()
    {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan("....abc");
        HashMap<String, Object> properties = new HashMap<String, Object>();
        properties.put("hibernate.hbm2ddl-auto", "none");
        properties.put("hibernate.ejb.entitymanager_factory_name", "entityManagerFactory");
        em.setJpaPropertyMap(properties);

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        return em;
    }

    @Primary
    @Bean(name = "transactionManager")
    public JpaTransactionManager transactionManager(@Qualifier("entityManagerFactory") final EntityManagerFactory factory)
    {
        return new JpaTransactionManager(factory);
    }

}

H2-DataSource的配置:

@Configuration
@EnableJpaRepositories(basePackages = "....def", entityManagerFactoryRef = "defEntityManagerFactory", transactionManagerRef = "defTransactionManager")
public class InavetDatabaseConfig
{

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.def")
    public DataSource defDataSource()
    {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "defEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean defEntityManagerFactory()
    {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(defDataSource());
        em.setPackagesToScan("....def");

        HashMap<String, Object> properties = new HashMap<String, Object>();
        properties.put("hibernate.hbm2ddl.auto", "create");
        properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        properties.put("hibernate.ejb.entitymanager_factory_name", "defEntityManagerFactory");
        em.setJpaPropertyMap(properties);

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        return em;
    }

    @Bean(name = "defTransactionManager")
    public JpaTransactionManager defTransactionManager(
            @Qualifier("defEntityManagerFactory") final EntityManagerFactory factory)
    {
        return new JpaTransactionManager(factory);
    }

}

共有1个答案

章涵容
2023-03-14

我发现,只有@primary标记的数据源加载fixture。对于这种行为,我的解决方案是向我的应用程序添加如下代码:

    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.setContinueOnError(true);
    populator.addScript(new PathResource("src/main/resources/data-h2.sql"));
    DataSource dataSource = (DataSource) cac.getBean("defDataSource");
    DatabasePopulatorUtils.execute(populator, dataSource);

其中,cac是ConfigurableApplicationContext的实例,通过以下方式作为返回值:springApplication.run(,);

 类似资料:
  • 我试图在Spring Boot(v1.2.3)应用程序中使用两个数据库连接,如文档(http://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/htmlsingle/#howto-two-datasources. 问题似乎是次要数据源是用主要数据源的属性构建的。 有人能指出我错过了什么吗? 应用特性:

  • 问题内容: 我对Spring Boot还是很陌生,我想为我的项目创建一个多数据源。这是我目前的情况。我有两个用于多个数据库的实体包。比方说 所以,目前我有两个实体类 UserMySql.java 我想实现一个配置,如果我从UserMySql创建用户,它将被保存到MySql数据库,如果我从Userh2创建用户,它将被保存到H2数据库。所以,我也有两个DBConfig,比如说MySqlDbConfig

  • 我有一个使用1.5.1版本spring boot的应用程序的问题。 启动程序时出现以下错误: 我尝试使用@primary,但当我需要使用其他数据源时,它不起作用。 谢谢你

  • 在我的spring boot应用程序中,我使用Oracle作为数据库。数据库具有不同的模式。我有一个要求,列出所有模式,并在循环中使用该模式连接到数据库,并对表执行一些查询。我看到一些博客通过在应用程序中定义数据源来连接到不同的模式。财产。我不想这样做,因为我已经定义了大约40个模式,我不想定义那么多的数据源。

  • 是否可以创建一个没有数据源的spring-boot应用程序?在我的情况下,我只需要一个简单的REST应用程序,但似乎在启动时,有一个尝试自动初始化一个数据源 我的pom.xml是 我的应用程序.属性是 当我跑的时候

  • 我需要你在这个问题上给我建议,在一个spring boot应用程序中,我从数据库加载一些属性,比如(cron periods,email data),我需要在应用程序上下文中导出这些属性,以便用加载的数据来spring构建相应的beans。我怎么能这样做?