简体   繁体   English

Spring Boot Multi Data sources: How to configure multi spring.jpa properties in java class

[英]Spring Boot Multi Data sources : How to configure multi spring.jpa properties in java class

application.yml应用程序.yml

spring:
  security:
    user:
      name: test
      password: admin 
  datasource:
    platform: postgres
    jdbc-url: jdbc:postgresql://localhost:5432/ktnb
    username: xxxx
    password: xxxx
    driverClassName: org.postgresql.Driver
  sqlserver-datasource:
    jdbc-url: jdbc:sqlserver://192.168.0.10;databaseName=backup1
    username: xxx
    password: xxx
    driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver

PrimaryDBConfig.java PrimaryDBConfig.java

package com.ktnb.keahlian.config;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
  entityManagerFactoryRef = "entityManagerFactory",
          transactionManagerRef = "transactionManager", 
  basePackages = { "com.ktnb.keahlian.repository" }
)
public class PrimaryDBConfig {
      
      @Primary
      @Bean(name = "dataSource")
      @ConfigurationProperties(prefix = "spring.datasource")
      public DataSource dataSource() {
        return DataSourceBuilder.create().build();
      }
      
      @Primary
      @Bean(name = "entityManagerFactory")
      public LocalContainerEntityManagerFactoryBean 
      entityManagerFactory(
        EntityManagerFactoryBuilder builder,
        @Qualifier("dataSource") DataSource dataSource
      ) {
          Map<String, Object> properties = new HashMap<String, Object>();
          properties.put("spring.jpa.database", "postgresql");
          properties.put("spring.jpa.show-sql", "true");
          properties.put("spring.jpa.hibernate.ddl-auto", "create");
          properties.put("spring.jpa.properties.hibernate.default_schema", "keahlian");
          properties.put("spring.jpa.org.hibernate.envers.default_schema", "keahlian_envers");
          properties.put("spring.jpa.org.hibernate.envers.audit_strategy", "org.hibernate.envers.strategy.ValidityAuditStrategy");
          
        return builder
          .dataSource(dataSource)
          .packages("com.ktnb.keahlian.entity")
          .persistenceUnit("primaryDB")
          .properties(properties)
          .build();
      }
        
      @Primary
      @Bean(name = "transactionManager")
      public PlatformTransactionManager transactionManager(
        @Qualifier("entityManagerFactory") EntityManagerFactory 
        entityManagerFactory
      ) {
        return new JpaTransactionManager(entityManagerFactory);
      }

}

But it cannot found right table/schema and not able show sql into console.但它找不到正确的表/模式并且无法在控制台中显示 sql。

Primary datasource for development DB.开发数据库的主要数据源。

Secondary datasource for Production DB.生产数据库的辅助数据源。

[INFO ] 2020-08-21 09:38:30.116 [http-nio-8080-exec-1] SessionListenerImpl - ==== Session is created ====
[INFO ] 2020-08-21 09:38:30.116 [http-nio-8080-exec-1] SessionListenerImpl - Total active session are 1
[WARN ] 2020-08-21 09:38:32.480 [http-nio-8080-exec-6] SqlExceptionHelper - SQL Error: 0, SQLState: 42P01
[ERROR] 2020-08-21 09:38:32.480 [http-nio-8080-exec-6] SqlExceptionHelper - ERROR: relation "pengguna" does not exist
  Position: 467

Can you try this?你能试试这个吗?

application.properties应用程序属性

primary.url={primary-database-url}      
primary.username={primary-database-username} 
primary.password={primary-database-password} 
primary.driver-class-name=com.mysql.jdbc.Driver 
primary.test-on-borrow=true            
primary.validation-query=SELECT 1   

secondary.url={secondary-database-url}
secondary.username={secondary-database-username}
secondary.password={secondary-database-password}
secondary.driver-class-name=com.mysql.jdbc.Driver
secondary.test-on-borrow=true
secondary.validation-query=SELECT 1
secondary.validation-interval=25200000

Create the configuration beans创建配置bean

@Bean(name = "primaryDataSource")
@ConfigurationProperties("primary")
@Primary
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean(name = "secondaryDataSource")
@ConfigurationProperties("secondary")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}

Next, create the JdbcTemplate beans that we are going to use for accessing the data sources in our data access layer.接下来,创建我们将用于访问数据访问层中的数据源的 JdbcTemplate bean。

@Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(@Qualifier("primary") DataSource primaryDs) {
        return new JdbcTemplate(writeDs);
}
    
@Bean(name = “secondaryJdbcTemplate")
    public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondary") DataSource secondaryDs) {
        return new JdbcTemplate(secondaryDs);
}

Try accessing properties like this:尝试访问这样的属性:

@Resource(name = "primaryJdbcTemplate")
private JdbcTemplate primaryJdbcTemplate;

@Resource(name = "secondaryJdbcTemplate")
private JdbcTemplate secondaryJdbcTemplate;


primaryJdbcTemplate.query(“any-query-to-apply-on-primary-data-source”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM