Skip to content

Instantly share code, notes, and snippets.

Avatar

Jay Ehsaniara ehsaniara

  • Los Angeles, CA
View GitHub Profile
View CustomerServiceImpl.java
package com.ehsaniara.multidatasource.service;
import com.ehsaniara.multidatasource.model.Customer;
import com.ehsaniara.multidatasource.repository.readRepository.CustomerReadRepository;
import com.ehsaniara.multidatasource.repository.writeRepository.CustomerWriteRepository;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.Optional;
View DemoApplication.java
package com.ehsaniara.multidatasource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Properties;
@SpringBootApplication
public class DemoApplication {
View CustomerReadRepository.java
package com.ehsaniara.multidatasource.repository.readRepository;
import com.ehsaniara.multidatasource.model.Customer;
import org.springframework.data.repository.CrudRepository;
/**
* @author Jay Ehsaniara, Dec 30 2019
*/
public interface CustomerReadRepository extends CrudRepository<Customer, Long> {
}
View CustomerWriteRepository.java
package com.ehsaniara.multidatasource.repository.writeRepository;
import com.ehsaniara.multidatasource.model.Customer;
import org.springframework.data.repository.CrudRepository;
/**
* @author Jay Ehsaniara, Dec 30 2019
*/
public interface CustomerWriteRepository extends CrudRepository<Customer, Long> {
}
View DataSourceConfigRead.java
package com.ehsaniara.multidatasource.configurations;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.boot.context.properties.ConfigurationProperties;
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;
View DataSourceConfigWrite.java
package com.ehsaniara.multidatasource.configurations;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.boot.context.properties.ConfigurationProperties;
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;
View application.yml
spring:
datasource-write:
driver-class-name: org.postgresql.Driver
jdbc-url: jdbc:postgresql://localhost:5432/demo
username: 'postgres'
password: 'postgres_user_for_db_write'
platform: postgresql
hikari:
idle-timeout: 10000
maximum-pool-size: 10
View pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>