Skip to content

Commit 36e107d

Browse files
BAEL-6525: changes for Set up Multiple Database with Flyway in Spring Boot (#18727)
* BAEL-6525: changes for Set up Multiple Database with Flyway in Spring Boot * addressing all the PR comments * updating correct props of ddl-auto * refactoring * updating app config * comments addressed --------- Co-authored-by: sverma1-godaddy <[email protected]>
1 parent d5e47ce commit 36e107d

File tree

16 files changed

+413
-0
lines changed

16 files changed

+413
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>flyway-multidb-springboot</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>flyway-multidb-springboot</name>
9+
<description>This is simple boot application for Spring boot multiple flyway database</description>
10+
11+
<parent>
12+
<groupId>com.baeldung.spring-boot-modules</groupId>
13+
<artifactId>spring-boot-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-data-jpa</artifactId>
21+
<version>3.2.3</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.flywaydb</groupId>
25+
<artifactId>flyway-core</artifactId>
26+
<version>9.22.3</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.h2database</groupId>
30+
<artifactId>h2</artifactId>
31+
<version>2.2.224</version>
32+
<scope>runtime</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<version>3.2.3</version>
38+
<scope>test</scope>
39+
</dependency>
40+
</dependencies>
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-maven-plugin</artifactId>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
51+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication(scanBasePackages = "com.baeldung")
7+
public class FlywayMultidbApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(FlywayMultidbApplication.class, args);
11+
}
12+
13+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.config;
2+
3+
import jakarta.annotation.PostConstruct;
4+
import javax.sql.DataSource;
5+
import java.util.Map;
6+
import jakarta.persistence.EntityManagerFactory;
7+
import org.flywaydb.core.Flyway;
8+
import org.springframework.boot.jdbc.DataSourceBuilder;
9+
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
10+
import org.springframework.context.annotation.*;
11+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
12+
import org.springframework.orm.jpa.*;
13+
import org.springframework.transaction.PlatformTransactionManager;
14+
import org.springframework.transaction.annotation.EnableTransactionManagement;
15+
16+
@Configuration
17+
@EnableTransactionManagement
18+
@EnableJpaRepositories(
19+
basePackages = "com.baeldung.repository.product",
20+
entityManagerFactoryRef = "productEntityManagerFactory",
21+
transactionManagerRef = "productTransactionManager"
22+
)
23+
public class ProductDbConfig {
24+
25+
@Bean
26+
public DataSource productDataSource() {
27+
return DataSourceBuilder.create()
28+
.url("jdbc:h2:mem:productdb")
29+
.username("sa")
30+
.password("")
31+
.driverClassName("org.h2.Driver")
32+
.build();
33+
}
34+
35+
@Bean
36+
public LocalContainerEntityManagerFactoryBean productEntityManagerFactory(
37+
EntityManagerFactoryBuilder builder) {
38+
39+
return builder
40+
.dataSource(productDataSource())
41+
.packages("com.baeldung.entity")
42+
.persistenceUnit("productPU")
43+
.properties(Map.of("hibernate.hbm2ddl.auto", "none"))
44+
.build();
45+
}
46+
47+
@Bean
48+
public PlatformTransactionManager productTransactionManager(
49+
EntityManagerFactory productEntityManagerFactory) {
50+
51+
return new JpaTransactionManager(productEntityManagerFactory);
52+
}
53+
54+
@PostConstruct
55+
public void migrateProductDb() {
56+
Flyway.configure()
57+
.dataSource(productDataSource())
58+
.locations("classpath:db/migration/productdb")
59+
.load()
60+
.migrate();
61+
}
62+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.config;
2+
3+
import jakarta.annotation.PostConstruct;
4+
import jakarta.persistence.EntityManagerFactory;
5+
import org.flywaydb.core.Flyway;
6+
import java.util.Map;
7+
import org.springframework.boot.jdbc.DataSourceBuilder;
8+
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.context.annotation.Primary;
12+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
13+
import org.springframework.orm.jpa.JpaTransactionManager;
14+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
15+
import org.springframework.transaction.PlatformTransactionManager;
16+
import org.springframework.transaction.annotation.EnableTransactionManagement;
17+
18+
import javax.sql.DataSource;
19+
20+
@Configuration
21+
@EnableTransactionManagement
22+
@EnableJpaRepositories(
23+
basePackages = "com.baeldung.repository.user",
24+
entityManagerFactoryRef = "userEntityManagerFactory",
25+
transactionManagerRef = "userTransactionManager"
26+
)
27+
public class UserDbConfig {
28+
29+
@Bean
30+
@Primary
31+
public DataSource userDataSource() {
32+
return DataSourceBuilder.create()
33+
.url("jdbc:h2:mem:userdb")
34+
.username("sa")
35+
.password("")
36+
.driverClassName("org.h2.Driver")
37+
.build();
38+
}
39+
40+
@Bean
41+
@Primary
42+
public LocalContainerEntityManagerFactoryBean userEntityManagerFactory(
43+
EntityManagerFactoryBuilder builder) {
44+
45+
return builder
46+
.dataSource(userDataSource())
47+
.packages("com.baeldung.entity")
48+
.persistenceUnit("userPU")
49+
.properties(Map.of("hibernate.hbm2ddl.auto", "none"))
50+
.build();
51+
}
52+
53+
@Bean
54+
@Primary
55+
public PlatformTransactionManager userTransactionManager(
56+
EntityManagerFactory userEntityManagerFactory) {
57+
58+
return new JpaTransactionManager(userEntityManagerFactory);
59+
}
60+
61+
@PostConstruct
62+
public void migrateUserDb() {
63+
Flyway.configure()
64+
.dataSource(userDataSource())
65+
.locations("classpath:db/migration/userdb")
66+
.load()
67+
.migrate();
68+
}
69+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.entity;
2+
3+
import jakarta.persistence.*;
4+
5+
@Entity
6+
@Table(name = "products")
7+
public class Product {
8+
9+
@Id
10+
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_seq")
11+
private Long id;
12+
13+
private String name;
14+
15+
public Long getId() {
16+
return id;
17+
}
18+
19+
public void setId(Long id) {
20+
this.id = id;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.entity;
2+
3+
import jakarta.persistence.*;
4+
5+
@Entity
6+
@Table(name = "users")
7+
public class User {
8+
9+
@Id
10+
@GeneratedValue(strategy = GenerationType.IDENTITY)
11+
private Long id;
12+
13+
private String name;
14+
15+
16+
public Long getId() {
17+
return id;
18+
}
19+
20+
public void setId(Long id) {
21+
this.id = id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.repository.product;
2+
3+
import com.baeldung.entity.Product;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface ProductRepository extends JpaRepository<Product, Long> {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.repository.user;
2+
3+
import com.baeldung.entity.User;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface UserRepository extends JpaRepository<User, Long> {
7+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.service;
2+
3+
import com.baeldung.entity.Product;
4+
import com.baeldung.repository.product.ProductRepository;
5+
import org.springframework.stereotype.Service;
6+
import org.springframework.transaction.annotation.Transactional;
7+
8+
import java.util.Optional;
9+
10+
@Service
11+
public class ProductService {
12+
13+
private final ProductRepository repo;
14+
15+
public ProductService(ProductRepository repo) {
16+
this.repo = repo;
17+
}
18+
19+
@Transactional("productTransactionManager")
20+
public Product save(Product product) {
21+
return repo.save(product);
22+
}
23+
24+
public Optional<Product> findById(Long id) {
25+
return repo.findById(id);
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.service;
2+
3+
import com.baeldung.entity.User;
4+
import com.baeldung.repository.user.UserRepository;
5+
import org.springframework.stereotype.Service;
6+
import org.springframework.transaction.annotation.Transactional;
7+
8+
import java.util.Optional;
9+
10+
@Service
11+
public class UserService {
12+
13+
private final UserRepository repo;
14+
15+
public UserService(UserRepository repo) {
16+
this.repo = repo;
17+
}
18+
19+
@Transactional("userTransactionManager")
20+
public User save(User user) {
21+
return repo.save(user);
22+
}
23+
24+
public Optional<User> findById(Long id) {
25+
return repo.findById(id);
26+
}
27+
}

0 commit comments

Comments
 (0)