This guide shows how to configure
multiple Derby databases in a
Spring Boot project for handling different data sources.
1. Add Dependencies
In
pom.xml
:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2. Configure Multiple Data Sources
In
application.properties
:
# First database
spring.datasource.primary.url=jdbc:derby:/app/data/db1;create=true
spring.datasource.primary.driver-class-name=org.apache.derby.jdbc.EmbeddedDriver
spring.datasource.primary.username=sa
spring.datasource.primary.password=
# Second database
spring.datasource.secondary.url=jdbc:derby:/app/data/db2;create=true
spring.datasource.secondary.driver-class-name=org.apache.derby.jdbc.EmbeddedDriver
spring.datasource.secondary.username=sa
spring.datasource.secondary.password=
3. Create Config for Primary Database
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 javax.sql.DataSource;
@Configuration
@EnableJpaRepositories(
basePackages = "com.example.repo.primary",
entityManagerFactoryRef = "primaryEntityManager",
transactionManagerRef = "primaryTxManager"
)
public class PrimaryConfig {
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean primaryEntityManager(
EntityManagerFactoryBuilder builder, DataSource dataSource) {
return builder.dataSource(dataSource)
.packages("com.example.model.primary")
.persistenceUnit("primary").build();
}
@Bean
@Primary
public JpaTransactionManager primaryTxManager(
LocalContainerEntityManagerFactoryBean primaryEntityManager) {
return new JpaTransactionManager(primaryEntityManager.getObject());
}
}
4. Create Config for Secondary Database
@Configuration
@EnableJpaRepositories(
basePackages = "com.example.repo.secondary",
entityManagerFactoryRef = "secondaryEntityManager",
transactionManagerRef = "secondaryTxManager"
)
public class SecondaryConfig {
@Bean
public LocalContainerEntityManagerFactoryBean secondaryEntityManager(
EntityManagerFactoryBuilder builder, DataSource dataSource) {
return builder.dataSource(dataSource)
.packages("com.example.model.secondary")
.persistenceUnit("secondary").build();
}
@Bean
public JpaTransactionManager secondaryTxManager(
LocalContainerEntityManagerFactoryBean secondaryEntityManager) {
return new JpaTransactionManager(secondaryEntityManager.getObject());
}
}
5. Create Entities
com.example.model.primary.User
com.example.model.secondary.Order
Each mapped to its own repository package.
6. Test Multiple Connections
@RestController
@RequestMapping("/multi")
public class MultiController {
private final UserRepository userRepo;
private final OrderRepository orderRepo;
public MultiController(UserRepository userRepo, OrderRepository orderRepo) {
this.userRepo = userRepo;
this.orderRepo = orderRepo;
}
@PostMapping("/user")
public User addUser(@RequestBody User u) {
return userRepo.save(u);
}
@PostMapping("/order")
public Order addOrder(@RequestBody Order o) {
return orderRepo.save(o);
}
}
image quote pre code