Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
<packaging>jar</packaging>

<name>spring-boot-multiple-datasources</name>
<description>Demo project for Spring Boot</description>
<description>Using Multiple datasources with Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand All @@ -35,6 +35,11 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<!-- PostgreSQL JDBC driver -->
<dependency>
<groupId>org.postgresql</groupId>
Expand Down Expand Up @@ -84,6 +89,20 @@
<version>1.4.195</version>
<scope>test</scope>
</dependency>



<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/foobar/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
42 changes: 21 additions & 21 deletions src/main/java/com/foobar/BarDbConfig.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.foobar;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
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;
Expand All @@ -15,29 +12,32 @@
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "barEntityManagerFactory",
transactionManagerRef = "barTransactionManager", basePackages = {"com.foobar.bar.repo"})
transactionManagerRef = "barTransactionManager", basePackages = {"com.foobar.bar.repo"})
public class BarDbConfig {

@Bean(name = "barDataSource")
@ConfigurationProperties(prefix = "bar.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "barDataSource")
@ConfigurationProperties(prefix = "bar.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "barEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("barDataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.foobar.bar.domain").persistenceUnit("bar")
.build();
}
@Bean(name = "barEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("barDataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.foobar.bar.domain").persistenceUnit("bar")
.build();
}

@Bean(name = "barTransactionManager")
public PlatformTransactionManager barTransactionManager(
@Qualifier("barEntityManagerFactory") EntityManagerFactory barEntityManagerFactory) {
return new JpaTransactionManager(barEntityManagerFactory);
}
@Bean(name = "barTransactionManager")
public PlatformTransactionManager barTransactionManager(
@Qualifier("barEntityManagerFactory") EntityManagerFactory barEntityManagerFactory) {
return new JpaTransactionManager(barEntityManagerFactory);
}

}
35 changes: 18 additions & 17 deletions src/main/java/com/foobar/FooBarController.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
package com.foobar;

import com.foobar.bar.domain.Bar;
import com.foobar.bar.repo.BarRepository;
import com.foobar.foo.domain.Foo;
import com.foobar.foo.repo.FooRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.foobar.bar.domain.Bar;
import com.foobar.bar.repo.BarRepository;
import com.foobar.foo.domain.Foo;
import com.foobar.foo.repo.FooRepository;
import java.util.Optional;

@RestController
public class FooBarController {

private final FooRepository fooRepo;
private final BarRepository barRepo;
private final FooRepository fooRepo;
private final BarRepository barRepo;

@Autowired
FooBarController(FooRepository fooRepo, BarRepository barRepo) {
this.fooRepo = fooRepo;
this.barRepo = barRepo;
}
@Autowired
FooBarController(FooRepository fooRepo, BarRepository barRepo) {
this.fooRepo = fooRepo;
this.barRepo = barRepo;
}

@RequestMapping("/foobar/{id}")
public String fooBar(@PathVariable("id") Long id) {
Foo foo = fooRepo.findById(id);
Bar bar = barRepo.findById(id);
@RequestMapping("/foobar/{id}")
public String fooBar(@PathVariable("id") Long id) {
Optional<Foo> foo = fooRepo.findById(id);
Optional<Bar> bar = barRepo.findById(id);

return foo.getFoo() + " " + bar.getBar() + "!";
}
return foo.get().getFoo() + " " + bar.get().getBar() + "!";
}

}
48 changes: 24 additions & 24 deletions src/main/java/com/foobar/FooDbConfig.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.foobar;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
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;
Expand All @@ -16,31 +13,34 @@
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory",
basePackages = {"com.foobar.foo.repo"})
basePackages = {"com.foobar.foo.repo"})
public class FooDbConfig {

@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}

@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.foobar.foo.domain").persistenceUnit("foo")
.build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.foobar.foo.domain").persistenceUnit("foo")
.build();
}

@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
36 changes: 11 additions & 25 deletions src/main/java/com/foobar/bar/domain/Bar.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
package com.foobar.bar.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.*;

@Entity
@Table(name = "bar")
public class Bar {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="bar_id_seq")
@SequenceGenerator(name="bar_id_seq", sequenceName="bar_id_seq", allocationSize=1)
@Column(name = "ID")
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "bar_id_seq")
@SequenceGenerator(name = "bar_id_seq", sequenceName = "bar_id_seq", allocationSize = 1)
@Column(name = "ID")
private Long id;

@Column(name = "BAR")
private String bar;
@Column(name = "BAR")
private String bar;

Bar(String bar) {
this.bar = bar;
}

Bar() {
// Default constructor needed by JPA
}

public String getBar() {
return bar;
}
public String getBar() {
return bar;
}
}
6 changes: 1 addition & 5 deletions src/main/java/com/foobar/bar/repo/BarRepository.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.foobar.bar.repo;

import com.foobar.bar.domain.Bar;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.foobar.bar.domain.Bar;

@Repository
public interface BarRepository extends JpaRepository<Bar, Long> {

Bar findById(Long id);

}
36 changes: 11 additions & 25 deletions src/main/java/com/foobar/foo/domain/Foo.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
package com.foobar.foo.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.*;

@Entity
@Table(name = "foo")
public class Foo {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="foo_id_seq")
@SequenceGenerator(name="foo_id_seq", sequenceName="foo_id_seq", allocationSize=1)
@Column(name = "ID")
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "foo_id_seq")
@SequenceGenerator(name = "foo_id_seq", sequenceName = "foo_id_seq", allocationSize = 1)
@Column(name = "ID")
private Long id;

@Column(name = "FOO")
private String foo;
@Column(name = "FOO")
private String foo;

Foo(String foo) {
this.foo = foo;
}

Foo() {
// Default constructor needed by JPA
}

public String getFoo() {
return foo;
}
public String getFoo() {
return foo;
}

}
6 changes: 1 addition & 5 deletions src/main/java/com/foobar/foo/repo/FooRepository.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.foobar.foo.repo;

import com.foobar.foo.domain.Foo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.foobar.foo.domain.Foo;

@Repository
public interface FooRepository extends JpaRepository<Foo, Long> {

Foo findById(Long id);

}
10 changes: 4 additions & 6 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
spring.jpa.database=default

# PostgreSQL DB - "foo"
spring.datasource.url=jdbc:postgresql://localhost:5432/foo?currentSchema=public
spring.datasource.jdbc-url=jdbc:postgresql://localhost:5432/foo?currentSchema=public
spring.datasource.username=postgres
spring.datasource.password=123456
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.driverClassName=org.postgresql.Driver

# PostgreSQL DB - "bar"
bar.datasource.url=jdbc:postgresql://localhost:5432/bar?currentSchema=public
bar.datasource.jdbc-url=jdbc:postgresql://localhost:5432/bar?currentSchema=public
bar.datasource.username=postgres
bar.datasource.password=123456
bar.datasource.driver-class-name=org.postgresql.Driver
bar.datasource.driverClassName=org.postgresql.Driver
Loading