Добрый день.
Вот, как это делаю я:
1) yaml конфигурация
spring:
datasource:
db_1:
url: jdbc:postgresql://localhost:5432/DB_NAME
jdbc-url: jdbc:postgresql://localhost:5432/DB_NAME
username: username
password: password
driver-class-name: org.postgresql.Driver
jpa:
show-sql: true
open-in-view: false
generate-ddl: true
database-platform: org.hibernate.dialect.PostgreSQL10Dialect
properties:
hibernate:
format_sql: true
order_inserts: true
order_updates: true
jdbc:
batch_size: 15
globally_quoted_identifiers: true
globally_quoted_identifiers_skip_column_definitions: true
query:
plan_cache_max_size: 4096
#fail_on_pagination_over_collection_fetch: true
in_clause_parameter_padding: true
hibernate:
ddl-auto: update
db_2:
url: jdbc:postgresql://localhost:5432/DB_NAME_2
jdbc-url: jdbc:postgresql://localhost:5432/DB_NAME_2
username: username
password: password
driver-class-name: org.postgresql.Driver
jpa:
show-sql: true
open-in-view: false
generate-ddl: true
database-platform: org.hibernate.dialect.PostgreSQL10Dialect
properties:
hibernate:
format_sql: true
order_inserts: true
order_updates: true
jdbc:
batch_size: 15
globally_quoted_identifiers: true
globally_quoted_identifiers_skip_column_definitions: true
query:
plan_cache_max_size: 4096
#fail_on_pagination_over_collection_fetch: true
in_clause_parameter_padding: true
hibernate:
ddl-auto: update
jackson:
serialization:
FAIL_ON_EMPTY_BEANS: false
2) Java конфигурация для первой БД
@Configuration
@EnableTransactionManagement
@RequiredArgsConstructor
@EnableJpaRepositories(
basePackages = {"com.example.db1.repository"},
entityManagerFactoryRef = "db1EntityManagerFactory",
transactionManagerRef = "db1EntityManagerFactory"
)
public class DB1JpaConfig {
private final Environment env;
@Bean
@Primary
@ConfigurationProperties("spring.datasource.db1")
public DataSourceProperties db1DataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "db1DataSource")
@Primary
public DataSource db1DataSource() {
return db1DataSourceProperties()
.initializeDataSourceBuilder()
.build();
}
@Bean(name = "db1EntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean db1EntityManagerFactory(
@Qualifier("db1DataSource") DataSource dataSource,
EntityManagerFactoryBuilder builder) {
HashMap<String, Object> properties = new HashMap<>();
properties.putAll(hibernateProperties());
properties.putAll(enversProperties());
return builder
.dataSource(dataSource)
.properties(properties)
.packages("com.example.db1.model")
.build();
}
/**
* Hibernate properties
* @return Map of properties for Hibernate
*/
private Map<String, Object> hibernateProperties() {
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("spring.datasource.db1.jpa.hibernate.ddl-auto"));
properties.put("hibernate.dialect", env.getProperty("spring.datasource.db1.jpa.database-platform"));
properties.put("hibernate.globally_quoted_identifiers", env.getProperty("spring.datasource.db1.jpa.properties.hibernate.globally_quoted_identifiers"));
properties.put("hibernate.globally_quoted_identifiers_skip_column_definitions", env.getProperty("spring.datasource.db1.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions"));
properties.put("hibernate.default_schema", env.getProperty("spring.datasource.db1.jpa.properties.hibernate.default_schema"));
properties.put("hibernate.format_sql", env.getProperty("spring.datasource.db1.jpa.properties.hibernate.format_sql"));
properties.put("hibernate.show_sql", env.getProperty("spring.datasource.db1.jpa.show-sql"));
properties.put("hibernate.order_inserts", env.getProperty("spring.datasource.db1.jpa.properties.hibernate.order_inserts"));
properties.put("hibernate.order_updates", env.getProperty("spring.datasource.db1.jpa.properties.hibernate.order_updates"));
properties.put("hibernate.jdbc.batch_size", env.getProperty("spring.datasource.db1.jpa.properties.hibernate.jdbc.batch_size"));
properties.put("spring.jpa.open-in-view", env.getProperty("spring.datasource.db1.jpa.open-in-view"));
properties.put("spring.jpa.generate-ddl", env.getProperty("spring.datasource.db1.jpa.generate-ddl"));
return properties;
}
@Bean(name = "db1EntityManagerFactory")
@Primary
public PlatformTransactionManager db1EntityManagerFactory(
@Qualifier("db1EntityManagerFactory") LocalContainerEntityManagerFactoryBean db1EntityManagerFactory) {
return new JpaTransactionManager(Objects.requireNonNull(db1EntityManagerFactory.getObject()));
}
}
Java конфигурация для 2-й БД (аналогична первому практически)
@Configuration
@EnableTransactionManagement
@RequiredArgsConstructor
@EnableJpaRepositories(
basePackages = {"com.example.db2.repository"},
entityManagerFactoryRef = "db2EntityManagerFactory",
transactionManagerRef = "db2EntityManagerFactory"
)
public class DB2JpaConfig {
private final Environment env;
@Bean
@Primary
@ConfigurationProperties("spring.datasource.db2")
public DataSourceProperties db2DataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "db2DataSource")
@Primary
public DataSource db2DataSource() {
return db2DataSourceProperties()
.initializeDataSourceBuilder()
.build();
}
@Bean(name = "db2EntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean db2EntityManagerFactory(
@Qualifier("db2DataSource") DataSource dataSource,
EntityManagerFactoryBuilder builder) {
HashMap<String, Object> properties = new HashMap<>();
properties.putAll(hibernateProperties());
properties.putAll(enversProperties());
return builder
.dataSource(dataSource)
.properties(properties)
.packages("com.example.db2.model")
.build();
}
/**
* Hibernate properties
* @return Map of properties for Hibernate
*/
private Map<String, Object> hibernateProperties() {
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("spring.datasource.db2.jpa.hibernate.ddl-auto"));
properties.put("hibernate.dialect", env.getProperty("spring.datasource.db2.jpa.database-platform"));
properties.put("hibernate.globally_quoted_identifiers", env.getProperty("spring.datasource.db2.jpa.properties.hibernate.globally_quoted_identifiers"));
properties.put("hibernate.globally_quoted_identifiers_skip_column_definitions", env.getProperty("spring.datasource.db2.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions"));
properties.put("hibernate.default_schema", env.getProperty("spring.datasource.db2.jpa.properties.hibernate.default_schema"));
properties.put("hibernate.format_sql", env.getProperty("spring.datasource.db2.jpa.properties.hibernate.format_sql"));
properties.put("hibernate.show_sql", env.getProperty("spring.datasource.db2.jpa.show-sql"));
properties.put("hibernate.order_inserts", env.getProperty("spring.datasource.db2.jpa.properties.hibernate.order_inserts"));
properties.put("hibernate.order_updates", env.getProperty("spring.datasource.db2.jpa.properties.hibernate.order_updates"));
properties.put("hibernate.jdbc.batch_size", env.getProperty("spring.datasource.db2.jpa.properties.hibernate.jdbc.batch_size"));
properties.put("spring.jpa.open-in-view", env.getProperty("spring.datasource.db2.jpa.open-in-view"));
properties.put("spring.jpa.generate-ddl", env.getProperty("spring.datasource.db2.jpa.generate-ddl"));
return properties;
}
@Bean(name = "db2EntityManagerFactory")
@Primary
public PlatformTransactionManager db2EntityManagerFactory(
@Qualifier("db2EntityManagerFactory") LocalContainerEntityManagerFactoryBean db2EntityManagerFactory) {
return new JpaTransactionManager(Objects.requireNonNull(db2EntityManagerFactory.getObject()));
}
}
Когда делаете запрос к БД используйте эту аннотацию:
import org.springframework.transaction.annotation.Transactional;
Для первой БД (например, на уровне сервиса):
@Transactional(transactionManager = "db1TransactionManager")
или для второй БД
@Transactional(transactionManager = "db2TransactionManager")