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 new file mode 100644 index 000000000..8f8327975 --- /dev/null +++ b/jpa/boot-data-multipledatasources/src/main/java/com/example/multipledatasources/configuration/BaseDataSourceConfiguration.java @@ -0,0 +1,47 @@ +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 dbe330910..014d5b1f5 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 @@ -13,12 +13,8 @@ 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.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 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; @Configuration @@ -26,18 +22,11 @@ basePackageClasses = CardHolderRepository.class, entityManagerFactoryRef = "cardHolderEntityManagerFactory", transactionManagerRef = "cardHolderTransactionManager") -public class CardHolderDataSourceConfiguration { +public class CardHolderDataSourceConfiguration extends BaseDataSourceConfiguration { - private final PersistenceUnitManager persistenceUnitManager; - - public CardHolderDataSourceConfiguration(ObjectProvider persistenceUnitManager) { - this.persistenceUnitManager = persistenceUnitManager.getIfAvailable(); - } - - @Bean - @ConfigurationProperties("app.datasource.cardholder.jpa") - JpaProperties cardHolderJpaProperties() { - return new JpaProperties(); + public CardHolderDataSourceConfiguration( + ObjectProvider persistenceUnitManager, JpaProperties jpaProperties) { + super(persistenceUnitManager, jpaProperties); } @Bean @@ -56,9 +45,8 @@ DataSource cardholderDataSource() { } @Bean - LocalContainerEntityManagerFactoryBean cardHolderEntityManagerFactory( - JpaProperties cardHolderJpaProperties, DataSource cardholderDataSource) { - EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(cardHolderJpaProperties); + LocalContainerEntityManagerFactoryBean cardHolderEntityManagerFactory(DataSource cardholderDataSource) { + EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(); return builder.dataSource(cardholderDataSource) .packages(CardHolder.class) .build(); @@ -66,25 +54,6 @@ LocalContainerEntityManagerFactoryBean cardHolderEntityManagerFactory( @Bean PlatformTransactionManager cardHolderTransactionManager(EntityManagerFactory cardHolderEntityManagerFactory) { - return new JpaTransactionManager(cardHolderEntityManagerFactory); - } - - private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(JpaProperties cardHolderJpaProperties) { - JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(cardHolderJpaProperties); - return new EntityManagerFactoryBuilder( - jpaVendorAdapter, cardHolderJpaProperties.getProperties(), this.persistenceUnitManager); - } - - private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) { - 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; + return createTransactionManager(cardHolderEntityManagerFactory); } } 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 ac20e7bab..04f8f3dcd 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 @@ -14,33 +14,20 @@ 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.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 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; -import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration -@EnableTransactionManagement @EnableJpaRepositories( basePackageClasses = MemberRepository.class, entityManagerFactoryRef = "memberEntityManagerFactory", transactionManagerRef = "memberTransactionManager") -public class MemberDataSourceConfiguration { +public class MemberDataSourceConfiguration extends BaseDataSourceConfiguration { - private final PersistenceUnitManager persistenceUnitManager; - - public MemberDataSourceConfiguration(ObjectProvider persistenceUnitManager) { - this.persistenceUnitManager = persistenceUnitManager.getIfAvailable(); - } - - @Bean - @ConfigurationProperties("app.datasource.member.jpa") - JpaProperties memberJpaProperties() { - return new JpaProperties(); + public MemberDataSourceConfiguration( + ObjectProvider persistenceUnitManager, JpaProperties jpaProperties) { + super(persistenceUnitManager, jpaProperties); } @Bean @@ -59,34 +46,14 @@ DataSource memberDataSource() { } @Bean - LocalContainerEntityManagerFactoryBean memberEntityManagerFactory( - JpaProperties memberJpaProperties, DataSource memberDataSource) { - EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(memberJpaProperties); + LocalContainerEntityManagerFactoryBean memberEntityManagerFactory(DataSource memberDataSource) { + EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(); return builder.dataSource(memberDataSource).packages(Member.class).build(); } @Primary @Bean PlatformTransactionManager memberTransactionManager(EntityManagerFactory memberEntityManagerFactory) { - return new JpaTransactionManager(memberEntityManagerFactory); - } - - private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(JpaProperties memberJpaProperties) { - JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(memberJpaProperties); - return new EntityManagerFactoryBuilder( - jpaVendorAdapter, memberJpaProperties.getProperties(), this.persistenceUnitManager); - } - - private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) { - 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; + return createTransactionManager(memberEntityManagerFactory); } } diff --git a/jpa/boot-data-multipledatasources/src/main/resources/application.properties b/jpa/boot-data-multipledatasources/src/main/resources/application.properties index 4b2e36653..eaf1ae34c 100644 --- a/jpa/boot-data-multipledatasources/src/main/resources/application.properties +++ b/jpa/boot-data-multipledatasources/src/main/resources/application.properties @@ -10,19 +10,6 @@ app.datasource.member.configuration.idleTimeout=600000 app.datasource.member.configuration.maxLifetime=1800000 app.datasource.member.configuration.poolName=memberHikariPool app.datasource.member.configuration.autoCommit=false -app.datasource.member.jpa.hibernate.ddl-auto=validate -app.datasource.member.jpa.open-in-view=false -app.datasource.member.jpa.properties.hibernate.jdbc.time_zone=UTC -app.datasource.member.jpa.properties.hibernate.id.new_generator_mappings=true -app.datasource.member.jpa.properties.hibernate.id.optimizer.pooled.preferred=pooled-lo -app.datasource.member.jpa.properties.hibernate.generate_statistics=false -app.datasource.member.jpa.properties.hibernate.jdbc.batch_size=25 -app.datasource.member.jpa.properties.hibernate.order_inserts=true -app.datasource.member.jpa.properties.hibernate.order_updates=true -app.datasource.member.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch=true -app.datasource.member.jpa.properties.hibernate.query.in_clause_parameter_padding=true -app.datasource.member.jpa.properties.hibernate.query.plan_cache_max_size=4096 -app.datasource.member.jpa.properties.hibernate.connection.provider_disables_autocommit=true #card number (cardholder id, cardnumber) app.datasource.cardholder.url=jdbc:mysql://localhost:3306/cardholderdb?createDatabaseIfNotExist=true @@ -34,19 +21,21 @@ app.datasource.cardholder.hikari.idleTimeout=600000 app.datasource.cardholder.hikari.maxLifetime=1800000 app.datasource.cardholder.hikari.poolName=cardHolderHikariPool app.datasource.cardholder.hikari.autoCommit=false -app.datasource.cardholder.jpa.hibernate.ddl-auto=validate -app.datasource.cardholder.jpa.open-in-view=false -app.datasource.cardholder.jpa.properties.hibernate.jdbc.time_zone=UTC -app.datasource.cardholder.jpa.properties.hibernate.id.new_generator_mappings=true -app.datasource.cardholder.jpa.properties.hibernate.id.optimizer.pooled.preferred=pooled-lo -app.datasource.cardholder.jpa.properties.hibernate.generate_statistics=false -app.datasource.cardholder.jpa.properties.hibernate.jdbc.batch_size=25 -app.datasource.cardholder.jpa.properties.hibernate.order_inserts=true -app.datasource.cardholder.jpa.properties.hibernate.order_updates=true -app.datasource.cardholder.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch=true -app.datasource.cardholder.jpa.properties.hibernate.query.in_clause_parameter_padding=true -app.datasource.cardholder.jpa.properties.hibernate.query.plan_cache_max_size=4096 -app.datasource.cardholder.jpa.properties.hibernate.connection.provider_disables_autocommit=true + +## Common Properties +spring.jpa.hibernate.ddl-auto=validate +spring.jpa.open-in-view=false +spring.jpa.properties.hibernate.jdbc.time_zone=UTC +spring.jpa.properties.hibernate.id.new_generator_mappings=true +spring.jpa.properties.hibernate.id.optimizer.pooled.preferred=pooled-lo +spring.jpa.properties.hibernate.generate_statistics=false +spring.jpa.properties.hibernate.jdbc.batch_size=25 +spring.jpa.properties.hibernate.order_inserts=true +spring.jpa.properties.hibernate.order_updates=true +spring.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch=true +spring.jpa.properties.hibernate.query.in_clause_parameter_padding=true +spring.jpa.properties.hibernate.query.plan_cache_max_size=4096 +spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true ## Flyway uses mysql database spring.flyway.url=${app.datasource.cardholder.url} @@ -59,4 +48,4 @@ spring.liquibase.user=${app.datasource.member.username} spring.liquibase.password=${app.datasource.member.password} spring.mvc.problemdetails.enabled=true -spring.threads.virtual.enabled=true \ No newline at end of file +spring.threads.virtual.enabled=true diff --git a/jpa/boot-data-multipledatasources/src/main/resources/db/changelog/db.changelog-sql-db.sql b/jpa/boot-data-multipledatasources/src/main/resources/db/changelog/db.changelog-sql-db.sql index bd0a6f005..6123cb826 100644 --- a/jpa/boot-data-multipledatasources/src/main/resources/db/changelog/db.changelog-sql-db.sql +++ b/jpa/boot-data-multipledatasources/src/main/resources/db/changelog/db.changelog-sql-db.sql @@ -1,4 +1,4 @@ --liquibase formatted sql --changeset raja:create-sequence -CREATE SEQUENCE member_seq START WITH 100 INCREMENT BY 50; \ No newline at end of file +CREATE SEQUENCE member_seq START WITH 101 INCREMENT BY 50; \ No newline at end of file diff --git a/jpa/boot-data-multipledatasources/src/main/resources/db/changelog/db.changelog-xml-db.xml b/jpa/boot-data-multipledatasources/src/main/resources/db/changelog/db.changelog-xml-db.xml index a7bed119a..ec5c10ac8 100644 --- a/jpa/boot-data-multipledatasources/src/main/resources/db/changelog/db.changelog-xml-db.xml +++ b/jpa/boot-data-multipledatasources/src/main/resources/db/changelog/db.changelog-xml-db.xml @@ -1,7 +1,10 @@ + xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.20.xsd"> + + + @@ -9,8 +12,8 @@ - - + + \ No newline at end of file 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 e8601c6ac..ce2f031df 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 @@ -6,8 +6,3 @@ CREATE TABLE card_holder ( member_id VARCHAR(255) NOT NULL, card_number VARCHAR(255) NOT NULL ); - --- Requires only if sequence type is Auto -create table hibernate_sequence( - next_val INTEGER NOT null -);