Skip to content

Commit

Permalink
feat : Consolidated JPA properties under a common namespace for easie…
Browse files Browse the repository at this point in the history
…r configuration and maintenance. (#1307)

* feat : use common properties for both db

* refactor to reduce duplicate
  • Loading branch information
rajadilipkolli authored Jul 1, 2024
1 parent 8ecf7f3 commit 7fdb737
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -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> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,20 @@
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
@EnableJpaRepositories(
basePackageClasses = CardHolderRepository.class,
entityManagerFactoryRef = "cardHolderEntityManagerFactory",
transactionManagerRef = "cardHolderTransactionManager")
public class CardHolderDataSourceConfiguration {
public class CardHolderDataSourceConfiguration extends BaseDataSourceConfiguration {

private final PersistenceUnitManager persistenceUnitManager;

public CardHolderDataSourceConfiguration(ObjectProvider<PersistenceUnitManager> persistenceUnitManager) {
this.persistenceUnitManager = persistenceUnitManager.getIfAvailable();
}

@Bean
@ConfigurationProperties("app.datasource.cardholder.jpa")
JpaProperties cardHolderJpaProperties() {
return new JpaProperties();
public CardHolderDataSourceConfiguration(
ObjectProvider<PersistenceUnitManager> persistenceUnitManager, JpaProperties jpaProperties) {
super(persistenceUnitManager, jpaProperties);
}

@Bean
Expand All @@ -56,35 +45,15 @@ 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();
}

@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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> persistenceUnitManager) {
this.persistenceUnitManager = persistenceUnitManager.getIfAvailable();
}

@Bean
@ConfigurationProperties("app.datasource.member.jpa")
JpaProperties memberJpaProperties() {
return new JpaProperties();
public MemberDataSourceConfiguration(
ObjectProvider<PersistenceUnitManager> persistenceUnitManager, JpaProperties jpaProperties) {
super(persistenceUnitManager, jpaProperties);
}

@Bean
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand All @@ -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
spring.threads.virtual.enabled=true
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--liquibase formatted sql

--changeset raja:create-sequence
CREATE SEQUENCE member_seq START WITH 100 INCREMENT BY 50;
CREATE SEQUENCE member_seq START WITH 101 INCREMENT BY 50;
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.20.xsd">

<property name="string.type" value="varchar(255)" dbms="!postgresql"/>
<property name="string.type" value="text" dbms="postgresql"/>

<changeSet author="raja" id="create-table">

<createTable tableName="member">
<column name="id" type="BIGINT">
<constraints nullable="false" primaryKey="true" primaryKeyName="pk_member"/>
</column>
<column name="name" type="VARCHAR(255)"/>
<column name="member_id" type="VARCHAR(255)"/>
<column name="name" type="${string.type}"/>
<column name="member_id" type="${string.type}"/>
</createTable>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

0 comments on commit 7fdb737

Please sign in to comment.