diff --git a/jpa/boot-data-multipledatasources/docker-compose.yml b/jpa/boot-data-multipledatasources/docker-compose.yml index fde48d7ce..2f98a4fbd 100644 --- a/jpa/boot-data-multipledatasources/docker-compose.yml +++ b/jpa/boot-data-multipledatasources/docker-compose.yml @@ -1,7 +1,7 @@ version: '3.8' services: postgres: - image: postgres:17.0-alpine + image: postgres:17.2-alpine ports: - "5432:5432" environment: diff --git a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/bootstrap/DataInitilizer.java b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/bootstrap/DataInitializer.java similarity index 86% rename from jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/bootstrap/DataInitilizer.java rename to jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/bootstrap/DataInitializer.java index 57df2688c..a62be0665 100644 --- a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/bootstrap/DataInitilizer.java +++ b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/bootstrap/DataInitializer.java @@ -4,7 +4,6 @@ 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; @@ -12,12 +11,16 @@ @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(); diff --git a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/BaseDataSourceConfiguration.java b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/BaseDataSourceConfiguration.java deleted file mode 100644 index 8f8327975..000000000 --- a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/BaseDataSourceConfiguration.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.example.multipledatasources.configuration; - -import jakarta.persistence.EntityManagerFactory; -import org.springframework.beans.factory.ObjectProvider; -import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; -import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; -import org.springframework.orm.jpa.JpaTransactionManager; -import org.springframework.orm.jpa.JpaVendorAdapter; -import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager; -import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter; -import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; -import org.springframework.transaction.PlatformTransactionManager; - -public abstract class BaseDataSourceConfiguration { - - protected final PersistenceUnitManager persistenceUnitManager; - protected final JpaProperties jpaProperties; - - protected BaseDataSourceConfiguration( - ObjectProvider persistenceUnitManager, JpaProperties jpaProperties) { - this.persistenceUnitManager = persistenceUnitManager.getIfAvailable(); - this.jpaProperties = jpaProperties; - } - - protected EntityManagerFactoryBuilder createEntityManagerFactoryBuilder() { - JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(); - return new EntityManagerFactoryBuilder( - jpaVendorAdapter, jpaProperties.getProperties(), this.persistenceUnitManager); - } - - protected JpaVendorAdapter createJpaVendorAdapter() { - AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); - adapter.setShowSql(jpaProperties.isShowSql()); - if (jpaProperties.getDatabase() != null) { - adapter.setDatabase(jpaProperties.getDatabase()); - } - if (jpaProperties.getDatabasePlatform() != null) { - adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform()); - } - adapter.setGenerateDdl(jpaProperties.isGenerateDdl()); - return adapter; - } - - protected PlatformTransactionManager createTransactionManager(EntityManagerFactory entityManagerFactory) { - return new JpaTransactionManager(entityManagerFactory); - } -} diff --git a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/CardHolderDataSourceConfiguration.java b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/CardHolderDataSourceConfiguration.java index 8726f9eb0..d51f59298 100644 --- a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/CardHolderDataSourceConfiguration.java +++ b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/CardHolderDataSourceConfiguration.java @@ -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, 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); } } diff --git a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/MemberDataSourceConfiguration.java b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/MemberDataSourceConfiguration.java index c97abdb43..748a6efaf 100644 --- a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/MemberDataSourceConfiguration.java +++ b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/MemberDataSourceConfiguration.java @@ -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, 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 {} diff --git a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/SwaggerConfig.java b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/SwaggerConfig.java index 2dcee7558..02d41d695 100644 --- a/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/SwaggerConfig.java +++ b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/SwaggerConfig.java @@ -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 {} diff --git a/jpa/boot-data-multipledatasources/src/main/resources/application.properties b/jpa/boot-data-multipledatasources/src/main/resources/application.properties index eaf1ae34c..4c3050f54 100644 --- a/jpa/boot-data-multipledatasources/src/main/resources/application.properties +++ b/jpa/boot-data-multipledatasources/src/main/resources/application.properties @@ -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 @@ -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 @@ -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 diff --git a/jpa/boot-data-multipledatasources/src/main/resources/db/migration/V1__Init.sql b/jpa/boot-data-multipledatasources/src/main/resources/db/migration/V1__Init.sql index ce2f031df..110eb844f 100644 --- a/jpa/boot-data-multipledatasources/src/main/resources/db/migration/V1__Init.sql +++ b/jpa/boot-data-multipledatasources/src/main/resources/db/migration/V1__Init.sql @@ -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 ); diff --git a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java index 61aef1a2a..ad8f76df2 100644 --- a/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java +++ b/jpa/boot-data-multipledatasources/src/test/java/com/example/multipledatasources/common/ContainersConfiguration.java @@ -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; } @@ -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); }; }