Skip to content

Commit

Permalink
feat : configure multidatasource using qualifier and defaultCandidate…
Browse files Browse the repository at this point in the history
… as false (#1554)

* feat : configure multidatasource using qualifier and defaultCandidate as false

* adds missing config file
  • Loading branch information
rajadilipkolli authored Dec 5, 2024
1 parent 10bfb2f commit fd963de
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 144 deletions.
2 changes: 1 addition & 1 deletion jpa/boot-data-multipledatasources/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.8'
services:
postgres:
image: postgres:17.0-alpine
image: postgres:17.2-alpine
ports:
- "5432:5432"
environment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
import com.example.multipledatasources.entities.member.Member;
import com.example.multipledatasources.repository.cardholder.CardHolderRepository;
import com.example.multipledatasources.repository.member.MemberRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class DataInitilizer {
class DataInitializer {

private final CardHolderRepository cardHolderRepository;
private final MemberRepository memberRepository;

DataInitializer(CardHolderRepository cardHolderRepository, MemberRepository memberRepository) {
this.cardHolderRepository = cardHolderRepository;
this.memberRepository = memberRepository;
}

@EventListener(ApplicationReadyEvent.class)
void loadInitialData() {
this.cardHolderRepository.deleteAll();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,51 @@
import com.zaxxer.hikari.HikariDataSource;
import jakarta.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
@Configuration(proxyBeanMethods = false)
@EnableJpaRepositories(
basePackageClasses = CardHolderRepository.class,
entityManagerFactoryRef = "cardHolderEntityManagerFactory",
transactionManagerRef = "cardHolderTransactionManager")
public class CardHolderDataSourceConfiguration extends BaseDataSourceConfiguration {
class CardHolderDataSourceConfiguration {

public CardHolderDataSourceConfiguration(
ObjectProvider<PersistenceUnitManager> persistenceUnitManager, JpaProperties jpaProperties) {
super(persistenceUnitManager, jpaProperties);
}

@Bean
@Qualifier("mysql") @Bean(defaultCandidate = false)
@ConfigurationProperties("app.datasource.cardholder")
DataSourceProperties cardHolderDataSourceProperties() {
return new DataSourceProperties();
}

@Bean
@Qualifier("mysql") @Bean(defaultCandidate = false)
@ConfigurationProperties("app.datasource.cardholder.hikari")
DataSource cardholderDataSource() {
return cardHolderDataSourceProperties()
DataSource cardholderDataSource(@Qualifier("mysql") DataSourceProperties dataSourceProperties) {
return dataSourceProperties
.initializeDataSourceBuilder()
.type(HikariDataSource.class)
.build();
}

@Bean
LocalContainerEntityManagerFactoryBean cardHolderEntityManagerFactory(DataSource cardholderDataSource) {
EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder();
return builder.dataSource(cardholderDataSource)
@Qualifier("mysql") @Bean(defaultCandidate = false)
LocalContainerEntityManagerFactoryBean cardHolderEntityManagerFactory(
@Qualifier("mysql") DataSource dataSource, EntityManagerFactoryBuilder builder) {
return builder.dataSource(dataSource)
.packages(CardHolder.class)
.persistenceUnit("cardholder")
.build();
}

@Bean
PlatformTransactionManager cardHolderTransactionManager(EntityManagerFactory cardHolderEntityManagerFactory) {
return createTransactionManager(cardHolderEntityManagerFactory);
@Qualifier("mysql") @Bean(defaultCandidate = false)
PlatformTransactionManager cardHolderTransactionManager(
@Qualifier("mysql") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,11 @@

import com.example.multipledatasources.entities.member.Member;
import com.example.multipledatasources.repository.member.MemberRepository;
import com.zaxxer.hikari.HikariDataSource;
import jakarta.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.autoconfigure.domain.EntityScan;
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.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
@EnableJpaRepositories(
basePackageClasses = MemberRepository.class,
entityManagerFactoryRef = "memberEntityManagerFactory",
transactionManagerRef = "memberTransactionManager")
public class MemberDataSourceConfiguration extends BaseDataSourceConfiguration {

public MemberDataSourceConfiguration(
ObjectProvider<PersistenceUnitManager> persistenceUnitManager, JpaProperties jpaProperties) {
super(persistenceUnitManager, jpaProperties);
}

@Bean
@ConfigurationProperties("app.datasource.member")
DataSourceProperties memberDataSourceProperties() {
return new DataSourceProperties();
}

@Bean
@ConfigurationProperties("app.datasource.member.configuration")
DataSource memberDataSource() {
return memberDataSourceProperties()
.initializeDataSourceBuilder()
.type(HikariDataSource.class)
.build();
}

@Bean
LocalContainerEntityManagerFactoryBean memberEntityManagerFactory(DataSource memberDataSource) {
EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder();
return builder.dataSource(memberDataSource).packages(Member.class).build();
}

@Primary
@Bean
PlatformTransactionManager memberTransactionManager(EntityManagerFactory memberEntityManagerFactory) {
return createTransactionManager(memberEntityManagerFactory);
}
}
@Configuration(proxyBeanMethods = false)
@EntityScan(basePackageClasses = Member.class)
@EnableJpaRepositories(basePackageClasses = MemberRepository.class)
class MemberDataSourceConfiguration {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import io.swagger.v3.oas.annotations.servers.Server;
import org.springframework.context.annotation.Configuration;

@Configuration
@Configuration(proxyBeanMethods = false)
@OpenAPIDefinition(
info = @Info(title = "spring-boot-multipledatasources", version = "v1"),
servers = @Server(url = "/"))
public class SwaggerConfig {}
class SwaggerConfig {}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
spring.application.name=boot-data-multipledatasources

#Store card holder (cardholder name, zip)
app.datasource.member.url=jdbc:postgresql://localhost:5432/memberdb?createDatabaseIfNotExist=true
app.datasource.member.username=postgres
app.datasource.member.password=postgres
app.datasource.member.driverClassName=org.postgresql.Driver
app.datasource.member.configuration.connectionTimeout=30000
app.datasource.member.configuration.idleTimeout=600000
app.datasource.member.configuration.maxLifetime=1800000
app.datasource.member.configuration.poolName=memberHikariPool
app.datasource.member.configuration.autoCommit=false
#Store cardholder (cardholder name, zip)
spring.datasource.url=jdbc:postgresql://localhost:5432/memberdb?createDatabaseIfNotExist=true
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.hikari.connectionTimeout=30000
spring.datasource.hikari.idleTimeout=600000
spring.datasource.hikari.maxLifetime=1800000
spring.datasource.hikari.poolName=memberHikariPool
spring.datasource.hikari.data-source-properties.ApplicationName=${spring.application.name}
spring.datasource.hikari.autoCommit=false

#card number (cardholder id, cardnumber)
app.datasource.cardholder.url=jdbc:mysql://localhost:3306/cardholderdb?createDatabaseIfNotExist=true
Expand All @@ -20,6 +21,7 @@ app.datasource.cardholder.hikari.connectionTimeout=30000
app.datasource.cardholder.hikari.idleTimeout=600000
app.datasource.cardholder.hikari.maxLifetime=1800000
app.datasource.cardholder.hikari.poolName=cardHolderHikariPool
app.datasource.cardholder.hikari.data-source-properties.ApplicationName=${spring.application.name}
app.datasource.cardholder.hikari.autoCommit=false

## Common Properties
Expand All @@ -43,9 +45,9 @@ spring.flyway.user=${app.datasource.cardholder.username}
spring.flyway.password=${app.datasource.cardholder.password}

## Liquibase used postgres Database
spring.liquibase.url=${app.datasource.member.url}
spring.liquibase.user=${app.datasource.member.username}
spring.liquibase.password=${app.datasource.member.password}
spring.liquibase.url=${spring.datasource.url}
spring.liquibase.user=${spring.datasource.username}
spring.liquibase.password=${spring.datasource.password}

spring.mvc.problemdetails.enabled=true
spring.threads.virtual.enabled=true
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-- DDL
CREATE TABLE card_holder (
id INT AUTO_INCREMENT PRIMARY KEY,
id BIGINT AUTO_INCREMENT PRIMARY KEY,
member_id VARCHAR(255) NOT NULL,
card_number VARCHAR(255) NOT NULL
);
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ MySQLContainer<?> mySQLContainer() {
@Bean
PostgreSQLContainer<?> postgreSQLContainer() {
postgreSQLContainer =
new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("17.0-alpine"));
new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("17.2-alpine"));
postgreSQLContainer.start();
return postgreSQLContainer;
}
Expand All @@ -42,9 +42,9 @@ public DynamicPropertyRegistrar databaseProperties(
properties.add("app.datasource.cardholder.url", mySQLContainer::getJdbcUrl);
properties.add("app.datasource.cardholder.username", mySQLContainer::getUsername);
properties.add("app.datasource.cardholder.password", mySQLContainer::getPassword);
properties.add("app.datasource.member.url", postgreSQLContainer::getJdbcUrl);
properties.add("app.datasource.member.username", postgreSQLContainer::getUsername);
properties.add("app.datasource.member.password", postgreSQLContainer::getPassword);
properties.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
properties.add("spring.datasource.username", postgreSQLContainer::getUsername);
properties.add("spring.datasource.password", postgreSQLContainer::getPassword);
};
}

Expand Down

0 comments on commit fd963de

Please sign in to comment.