相关疑难解决方法(0)

Spring Boot配置和使用两个DataSource

我是Spring和Spring Boot的新手.如何配置和使用两个数据源.

例如,这是第一个数据源的内容.

application.properties

#first db
spring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver

#second db ...
Run Code Online (Sandbox Code Playgroud)

应用类

@SpringBootApplication
public class SampleApplication
{
    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何修改application.properties以添加其他数据源?如何将其自动装配以供其他仓库使用?

java spring spring-mvc spring-boot

174
推荐指数
6
解决办法
18万
查看次数

用于测试的嵌入式H2数据库的Spring配置

对于集成测试,您的Spring配置与使用嵌入式h2数据源以及可选的JUnit 相似?

我第一次尝试使用SingleConnectionDataSource基本上可以工作,但是在更复杂的测试中失败了,你需要同时连接多个连接或挂起事务.我认为基于tcp的服务器模式中的 h2 也可以正常工作,但这可能不是内存中临时嵌入式数据库的最快通信模式.

有哪些可能性及其优点/缺点?另外,如何创建表/填充数据库?


更新:让我们指定一些对此类测试很重要的具体要求.

  • 数据库应该是临时的并且在内存中
  • 对于速度要求,连接可能不应使用tcp
  • 如果我可以使用数据库工具在调试期间检查数据库的内容,那将是很好的
  • 我们必须定义数据源,因为我们不能在单元测试中使用应用程序服务器数据源

spring unit-testing h2 embedded-database

41
推荐指数
3
解决办法
7万
查看次数

如何java为Spring批量数据和业务数据配置单独的数据源?我应该这样做吗?

我的主要工作只是读取操作而另一个人写了一些但MyISAM engine忽略了事务,所以我不需要事务支持.如何配置Spring Batch拥有自己的数据源JobRepository,与持有业务数据的数据源分开?最初的一个数据源配置如下所示:

@Configuration
public class StandaloneInfrastructureConfiguration {

    @Autowired
    Environment env;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
      LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
      em.setDataSource(dataSource());
      em.setPackagesToScan(new String[] { "org.podcastpedia.batch.*" });

      JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
      em.setJpaVendorAdapter(vendorAdapter);
      em.setJpaProperties(additionalJpaProperties());

      return em;
    }

    Properties additionalJpaProperties() {
          Properties properties = new Properties();
          properties.setProperty("hibernate.hbm2ddl.auto", "none");
          properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
          properties.setProperty("hibernate.show_sql", "true");

          return properties;
    }

    @Bean
    public DataSource dataSource(){

       return DataSourceBuilder.create()
                .url(env.getProperty("db.url"))
                .driverClassName(env.getProperty("db.driver"))
                .username(env.getProperty("db.username"))
                .password(env.getProperty("db.password"))
                .build();          
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
      JpaTransactionManager transactionManager = …
Run Code Online (Sandbox Code Playgroud)

spring spring-batch spring-boot spring-java-config

27
推荐指数
6
解决办法
5万
查看次数