Skip to content

Commit 38e67b0

Browse files
committed
Spring Framework Balanced Reader (2 datasource) example
1 parent 2387494 commit 38e67b0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2960
-289
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Tutorial: Getting Started with the AWS JDBC Driver, Spring Boot and Hibernate for load-balanced read-write and read-only connections (Single Datasource)
2+
3+
In this tutorial, you will set up a Spring Boot and Hibernate application with the AWS Advanced JDBC Driver, and use a single datasource to fetch and update data from an Aurora PostgreSQL database. The datasource is configured to provide a writer connection or a reader connection to off-load a writer node from read-only queries. It provides pooled connections through the AWS Advanced JDBC Driver internal connection pool configuration.
4+
5+
> Note: this tutorial was written using the following technologies:
6+
> - Spring Boot 2.7.1
7+
> - Hibernate
8+
> - AWS JDBC Driver 2.3.2
9+
> - Postgresql 42.5.4
10+
> - Gradle 7
11+
> - Java 11
12+
13+
You will progress through the following sections:
14+
1. Create a Gradle Spring Boot project
15+
2. Add the required Gradle dependencies
16+
3. Configure the AWS Advanced JDBC Driver
17+
18+
## Pre-requisites
19+
- This tutorial uses the Amazon Aurora PostgreSQL database.
20+
21+
## Step 1: Create a Gradle Project
22+
Create a Gradle project with the following project hierarchy:
23+
24+
```
25+
└───src
26+
└───main
27+
├───java
28+
│ └───example
29+
│ ├───data
30+
│ │ ├───Book.java
31+
│ │ ├───BookRepository.java
32+
│ │ └───BookService.java
33+
│ └───spring
34+
│ ├───Config.java
35+
│ ├───ShouldRetryTransactionException.java
36+
│ └───SpringHibernateBalancedReaderOneDataSourceExampleApplication.java
37+
└───resources
38+
└───application.yml
39+
```
40+
41+
> Note: this sample code assumes the target database contains a table named `Book` that can be generated using the SQL queries provided in `src/main/resources/books.sql`.
42+
43+
## Step 2: Add the required Gradle Dependencies
44+
In your `build.gradle.kts`, add the following dependencies.
45+
46+
```
47+
dependencies {
48+
implementation("org.springframework.boot:spring-boot-starter-jdbc")
49+
implementation("org.springframework.retry:spring-retry")
50+
implementation("org.postgresql:postgresql")
51+
implementation("software.amazon.jdbc:aws-advanced-jdbc-wrapper:latest")
52+
}
53+
```
54+
55+
Please note that the sample code inside the AWS JDBC Driver project will use the dependency `implementation(project(":aws-advanced-jdbc-wrapper"))` instead of `implementation("software.amazon.jdbc:aws-advanced-jdbc-wrapper:latest")` as seen above.
56+
57+
## Step 3: Configure Spring and Hibernate
58+
Configure Spring to use the AWS JDBC Driver as the default datasource.
59+
60+
1. In the `application.yml`, add new datasources for Spring:
61+
```yaml
62+
spring:
63+
datasource:
64+
load-balanced-writer-and-reader-datasource:
65+
url: jdbc:aws-wrapper:postgresql://test-cluster.cluster-XYZ.us-east-2.rds.amazonaws.com:5432/postgres?wrapperProfileName=F0&readerHostSelectorStrategy=roundRobin
66+
username: dev_user
67+
password: dev_password
68+
driver-class-name: software.amazon.jdbc.Driver
69+
type: org.springframework.jdbc.datasource.SimpleDriverDataSource
70+
```
71+
2. The datasource mentioned above does not use Hikari datasource that is default for Spring 2+ application. The AWS JDBC Driver manages its own internal connection pool (or several connection pools, if needed), which increases overall efficiency and helps facilitate failover support. All necessary configuration parameters are defined in the `F0` configuration profile. Other configuration presets `D`, `E` and `F` can be used as well. Any configuration profile or preset specified should use the [Read/Write Splitting Plugin](../docs/using-the-jdbc-driver/using-plugins/UsingTheReadWriteSplittingPlugin.md). More details are available at [Configuration Profiles](../docs/using-the-jdbc-driver/UsingTheJdbcDriver.md#configuration-profiles) and [Configuration Presets](../docs/using-the-jdbc-driver/ConfigurationPresets.md).
72+
<br><br>
73+
Including the optional configuration parameter `readerHostSelectorStrategy` in the connection string helps to set up a strategy to select a reader node. Possible values are `random`, `roundRobin` and `leastConnections`. More details are available at [Reader Selection Strategies](../docs/using-the-jdbc-driver/using-plugins/UsingTheReadWriteSplittingPlugin.md#reader-selection-strategies).
74+
75+
76+
3. Configure Hibernate dialect:
77+
```yaml
78+
jpa:
79+
properties:
80+
hibernate:
81+
dialect: org.hibernate.dialect.PostgreSQLDialect
82+
```
83+
84+
4. [Optional] You can enable driver logging by adding the following to `application.yml`:
85+
```yaml
86+
logging:
87+
level:
88+
software:
89+
amazon:
90+
jdbc: INFO
91+
jdbc.states: INFO
92+
example: TRACE
93+
```
94+
95+
For detailed logs use `TRACE` for `software.amazon.jdbc` package.
96+
97+
Start the application by running `./gradlew :springhibernateonedatasource:bootRun` in the terminal. You should see the application making a connection to the database and fetching data from the Example table.
98+
99+
# Summary
100+
This tutorial walks through the steps required to add and configure the AWS Advanced JDBC Driver to a simple Spring Boot and Hibernate application.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
plugins {
18+
id("org.springframework.boot") version "2.7.0"
19+
id("io.spring.dependency-management") version "1.1.4"
20+
}
21+
22+
dependencies {
23+
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
24+
implementation("org.springframework.retry:spring-retry")
25+
implementation("org.postgresql:postgresql:42.7.1")
26+
implementation("software.amazon.awssdk:rds:2.22.13")
27+
implementation(project(":aws-advanced-jdbc-wrapper"))
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License").
4+
# You may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Do not publish the Jar file for this subproject
16+
nexus.publish=false

0 commit comments

Comments
 (0)