Skip to content
Merged
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
51 changes: 51 additions & 0 deletions spring-boot-modules/flyway-multidb-springboot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>flyway-multidb-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>flyway-multidb-springboot</name>
<description>This is simple boot application for Spring boot multiple flyway database</description>

<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>3.2.3</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>9.22.3</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>3.2.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = "com.baeldung")
public class FlywayMultidbApplication {

public static void main(String[] args) {
SpringApplication.run(FlywayMultidbApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.baeldung.config;

import jakarta.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.Map;
import jakarta.persistence.EntityManagerFactory;
import org.flywaydb.core.Flyway;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.*;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.*;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = "com.baeldung.repository.product",
entityManagerFactoryRef = "productEntityManagerFactory",
transactionManagerRef = "productTransactionManager"
)
public class ProductDbConfig {

@Bean
public DataSource productDataSource() {
return DataSourceBuilder.create()
.url("jdbc:h2:mem:productdb")
.username("sa")
.password("")
.driverClassName("org.h2.Driver")
.build();
}

@Bean
public LocalContainerEntityManagerFactoryBean productEntityManagerFactory(
EntityManagerFactoryBuilder builder) {

return builder
.dataSource(productDataSource())
.packages("com.baeldung.entity")
.persistenceUnit("productPU")
.properties(Map.of("hibernate.hbm2ddl.auto", "none"))
.build();
}

@Bean
public PlatformTransactionManager productTransactionManager(
EntityManagerFactory productEntityManagerFactory) {

return new JpaTransactionManager(productEntityManagerFactory);
}

@PostConstruct
public void migrateProductDb() {
Flyway.configure()
.dataSource(productDataSource())
.locations("classpath:db/migration/productdb")
.load()
.migrate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.baeldung.config;

import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManagerFactory;
import org.flywaydb.core.Flyway;
import java.util.Map;
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 javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = "com.baeldung.repository.user",
entityManagerFactoryRef = "userEntityManagerFactory",
transactionManagerRef = "userTransactionManager"
)
public class UserDbConfig {

@Bean
@Primary
public DataSource userDataSource() {
return DataSourceBuilder.create()
.url("jdbc:h2:mem:userdb")
.username("sa")
.password("")
.driverClassName("org.h2.Driver")
.build();
}

@Bean
@Primary
public LocalContainerEntityManagerFactoryBean userEntityManagerFactory(
EntityManagerFactoryBuilder builder) {

return builder
.dataSource(userDataSource())
.packages("com.baeldung.entity")
.persistenceUnit("userPU")
.properties(Map.of("hibernate.hbm2ddl.auto", "none"))
.build();
}

@Bean
@Primary
public PlatformTransactionManager userTransactionManager(
EntityManagerFactory userEntityManagerFactory) {

return new JpaTransactionManager(userEntityManagerFactory);
}

@PostConstruct
public void migrateUserDb() {
Flyway.configure()
.dataSource(userDataSource())
.locations("classpath:db/migration/userdb")
.load()
.migrate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.baeldung.entity;

import jakarta.persistence.*;

@Entity
@Table(name = "products")
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_seq")
private Long id;

private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baeldung.entity;

import jakarta.persistence.*;

@Entity
@Table(name = "users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;


public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.baeldung.repository.product;

import com.baeldung.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ProductRepository extends JpaRepository<Product, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.baeldung.repository.user;

import com.baeldung.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.service;

import com.baeldung.entity.Product;
import com.baeldung.repository.product.ProductRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class ProductService {

private final ProductRepository repo;

public ProductService(ProductRepository repo) {
this.repo = repo;
}

@Transactional("productTransactionManager")
public Product save(Product product) {
return repo.save(product);
}

public Optional<Product> findById(Long id) {
return repo.findById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.service;

import com.baeldung.entity.User;
import com.baeldung.repository.user.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class UserService {

private final UserRepository repo;

public UserService(UserRepository repo) {
this.repo = repo;
}

@Transactional("userTransactionManager")
public User save(User user) {
return repo.save(user);
}

public Optional<User> findById(Long id) {
return repo.findById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
spring:
datasource:
userdb:
url: jdbc:h2:mem:userdb
username: sa
password:
driver-class-name: org.h2.Driver
productdb:
url: jdbc:h2:mem:productdb
username: sa
password:
driver-class-name: org.h2.Driver
application:
name: flyway-multidb-springboot

flyway:
enabled: false
jpa:
defer-datasource-initialization: false
hibernate:
ddl-auto: none
main:
allow-circular-references: true
h2:
console:
enabled: true
path: /h2-console
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE products (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE users (
id BIGINT PRIMARY KEY,
name VARCHAR(255)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class FlywayMultidbApplicationTests {

@Test
void contextLoads() {
}

}
Loading