Spring Boot-Adding EntityListeners to application with multiple data sources

package com.medium.app.config;import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder.Builder;
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.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "db1EntityManagerFactory",
transactionManagerRef ="db1TransactionManager",
basePackages={"com.medium.app.repository.db1"}
)
public class Db1DAOConfig
{
@Bean(name = "db1DataSourceProperties")
@ConfigurationProperties("db1.datasource")
public DataSourceProperties db1DataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "db1DataSource")
public DataSource db1DataSource()
{
return db1DataSourceProperties().
initializeDataSourceBuilder().
type(BasicDataSource.class).build();

}


@Bean(name = "db1EntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
db1EntityManagerFactory(final EntityManagerFactoryBuilder builder)
{
Builder dataSource = builder.dataSource(db1DataSource());
return dataSource.
packages("com.medium.app.entity.db1").persistenceUnit("db1").build();
}


@Bean(name = "db1TransactionManager")
public PlatformTransactionManager db1TransactionManager(@Qualifier("db1EntityManagerFactory")EntityManagerFactory db1EntityManagerFactory)
{
return new JpaTransactionManager(db1EntityManagerFactory);
}
}
package com.medium.app.config;import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
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.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.zaxxer.hikari.HikariDataSource;@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "db2EntityManagerFactory",
transactionManagerRef="db2TransactionManager",
basePackages="com.medium.app.repository.db2"
)
public class Db2DAOConfig
{

@Bean(name = "db2DataSourceProperties")
@Primary
@ConfigurationProperties("db2.datasource")
public DataSourceProperties db2DataSourceProperties() {
return new DataSourceProperties();
}

@Primary
@Bean(name = "db2DataSource")
public DataSource db2DataSource()
{
return db2DataSourceProperties().
initializeDataSourceBuilder().
type(HikariDataSource.class).build();

}


@Primary
@Bean(name = "db2EntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("db2DataSource")DataSource dataSource){

return builder.dataSource(dataSource)
.packages("com.medium.app.entity.db2").
persistenceUnit("db2").build();
}

@Primary
@Bean(name="db2TransactionManager")
public PlatformTransactionManager db2TransactionManager(@Qualifier("db2EntityManagerFactory")EntityManagerFactory db2EntityManagerFactory)
{
return new JpaTransactionManager(db2EntityManagerFactory);
}



}
db2.datasource.driver = com.mysql.cj.jdbc.Driver
db2.datasource.url= jdbc:mysql://localhost:3306/db2
db2.datasource.username=root
db2.datasource.password=root
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=truedb1.datasource.driver=com.mysql.cj.jdbc.Driver
db1.datasource.url=jdbc:mysql://localhost:3306/db1
db1.datasource.username=root
db1.datasource.password=root
@Entity
@Table(name = "commission_details")
@EntityListeners(value=CommissionDetailsEntityListener.class)
public class CommissionDetails
{
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name = "domain_id")
private Long domainId;
@Column(name = "status")
private Long status;
}
package com.medium.app.entity.db2.listeners;import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
import javax.transaction.Transactional;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import static javax.transaction.Transactional.TxType.MANDATORY;
import com.medium.app.entity.db2.CommissionDetails;
import com.medium.app.entity.db2.CommissionDetailsAudit;
public class CommissionDetailsEntityListener
{
@PrePersist
public void prePersist(CommissionDetails commissionDetails)
{
perform(commissionDetails, "CREATE");
}

@PreUpdate
public void preUpdate(CommissionDetails commissionDetails)
{
perform(commissionDetails, "UPDATE");
}

@PreRemove
public void preRemove(CommissionDetails commissionDetails)
{
perform(commissionDetails, "DELETED");
}

@Transactional(MANDATORY)
private void perform(CommissionDetails commissionDetails, String action)
{
CommissionDetailsAudit commissionDetailsAudit = new CommissionDetailsAudit();
commissionDetailsAudit.setAction(action); commissionDetailsAudit.setDomainId(commissionDetails.getDomainId());
commissionDetailsAudit.setStatus(commissionDetails.getStatus());


Object entityManager = BeanUtil.getBean("db2EntityManagerFactory");
System.out.println(entityManager);

if(entityManager instanceof EntityManagerFactory)
{
EntityManagerFactory nativeEntityManagerFactory = (EntityManagerFactory) entityManager;
EntityManager entityManagerObj = nativeEntityManagerFactory.createEntityManager();
entityManagerObj.getTransaction().begin();
entityManagerObj.persist(commissionDetailsAudit);
entityManagerObj.getTransaction().commit();
entityManagerObj.close();
}



}
}
@Service
public class BeanUtil implements ApplicationContextAware
{
private static ApplicationContext context;
public static <T> T getBean(Class<T> beanClass)
{
return context.getBean(beanClass);
}
public static Object getBean(String beanName)
{
return context.getBean(beanName);
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
context = applicationContext;
}
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

CI/CD: Google Cloud Build — Regex Build Triggers

Kubernetes TLS bootstrapping

Tableau Version Control is here to make your life easier

Head First Java Chapter 6 Summery

Reactive Stream With Spring Boot

Your Manager Can’t Code? They Shouldn’t Be Your Manager

Attention: Faction War is Coming to Sipheria!

PYTHON for System Programming — Part 1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhruv Saksena

Dhruv Saksena

More from Medium

H2(In-Memory) Database With Spring Boot

Lets Know About Spring Boot..

Introduction to Spring boot

Microservices with Spring Boot Part 2