Spring Boot with AWS Aurora read replica

JackyNote ⭐️
2 min readSep 13, 2023

To integrate a Spring Boot application with AWS Aurora using different read and write endpoints, you’ll first need to set up your Aurora cluster with read and write endpoints. Once you have those, you can configure your Spring Boot application to use them. Below is an example code snippet demonstrating how to do this:

1. Add Dependencies:

<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- AWS SDK for Java -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
</dependency>
<!-- MySQL/Aurora JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>

2. Configure application.properties/application.yml:

# DataSource Configuration for Write (Primary) Endpoint
spring.datasource.url=jdbc:mysql://write-endpoint:3306/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password

# Hibernate Configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
# DataSource Configuration for Read (Replica) Endpoint
spring.datasource.read.url=jdbc:mysql://read-endpoint:3306/your_database
spring.datasource.read.username=your_username
spring.datasource.read.password=your_password

3. Create Configuration Class:

Create a configuration class to manage multiple datasources:

@Configuration
@EnableTransactionManagement
public class DataSourceConfig {

@Primary
@Bean(name = "writeDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "readDataSource")
@ConfigurationProperties(prefix = "spring.datasource.read")
public DataSource readDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("writeDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.yourpackage.model")
.persistenceUnit("primary")
.build();
}
@Bean(name = "readEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
readEntityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("readDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.yourpackage.model")
.persistenceUnit("read")
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean(name = "readTransactionManager")
public PlatformTransactionManager readTransactionManager(@Qualifier("readEntityManagerFactory") EntityManagerFactory readEntityManagerFactory) {
return new JpaTransactionManager(readEntityManagerFactory);
}
}

Define Repository Interfaces:
Create your JPA repositories, typically in com.yourpackage.repository.

Use Multiple Entity Managers:
In your services or controllers, use the @PersistenceContext annotation to specify which EntityManager you want to use for a particular operation.

@PersistenceContext(unitName = "primary")
private EntityManager writeEntityManager;

@PersistenceContext(unitName = "read")
private EntityManager readEntityManager;

With this setup, you can now perform read and write operations using different endpoints.

Remember to replace placeholders like your_database, your_username, your_password, com.yourpackage.model, and adjust the package structure and class names as per your project's actual setup.

Make sure you have the correct dependencies for your AWS SDK and that you have the necessary IAM permissions set up for your application to access your Aurora cluster.

First original publication on dev.to

--

--

JackyNote ⭐️

🚀 Software Engineer | Full Stack Java 6 Years of Experience | Tech Enthusiast | Startup Lover | Constantly Exploring the Latest Trends | Coffee🔍