Skip to content

Commit 0f8b5aa

Browse files
doc: Add tutorial and code samples with Spring and Hikari (#485)
1 parent 8b9b22d commit 0f8b5aa

File tree

10 files changed

+268
-3
lines changed

10 files changed

+268
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1818
- Sample code and tutorial for using the driver with:
1919
- [Spring and Hibernate](./examples/SpringHibernateExample/README.md)
2020
- [Spring and Wildfly](./examples/SpringWildflyExample/README.md)
21+
- [Spring and Hikari](./examples/SpringBootHikariExample/README.md)
2122

2223
### :bug: Fixed
2324
- Pruned null connections in connection tracker plugins ([PR #461](https://github.com/awslabs/aws-advanced-jdbc-wrapper/pull/461))
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Tutorial: Getting started with Spring Boot, Hikari, and the AWS Advanced JDBC Wrapper Driver
2+
3+
In this tutorial, you will set up a Spring Boot application using Hikari and the AWS Advanced JDBC Driver.
4+
5+
> Note: this tutorial was written using the following technologies:
6+
> - Spring Boot 2.7.0
7+
> - AWS Advanced JDBC Wrapper 2.2.0
8+
> - Postgresql 42.5.4
9+
> - Java 8
10+
11+
12+
## Step 1: Create a Gradle Project
13+
Create a Gradle Project with the following project hierarchy:
14+
```
15+
├───gradle
16+
│ └───wrapper
17+
│ ├───gradle-wrapper.jar
18+
│ └───gradle-wrapper.properties
19+
├───build.gradle.kts
20+
├───gradlew
21+
└───src
22+
└───main
23+
├───java
24+
│ └───software
25+
│ └───amazon
26+
│ └───SpringBootHikariExampleApplication.java
27+
└───resources
28+
└───application.yml
29+
```
30+
When creating the `SpringBootHikariExampleApplication.java` class, add the following code to it.
31+
32+
```java
33+
package software.amazon.SpringBootHikariExample;
34+
35+
import org.springframework.boot.SpringApplication;
36+
import org.springframework.boot.autoconfigure.SpringBootApplication;
37+
38+
@SpringBootApplication
39+
public class SpringBootHikariExampleApplication {
40+
public static void main(String[] args) {
41+
SpringApplication.run(SpringBootHikariExampleApplication.class, args);
42+
}
43+
}
44+
```
45+
You may also use the Spring Initializr to create the boilerplate code:
46+
1. Go to https://start.spring.io/
47+
2. Select the Maven project and version 2.7.9 of the Spring Boot.
48+
3. Select Java version 8.
49+
4. Click Dependencies and select the following:
50+
- Spring Web
51+
- Spring Data JDBC
52+
- PostgreSQL Driver
53+
54+
## Step 2: Add the required Gradle Dependencies
55+
56+
In the `build.gradle.kts` file, add the following dependencies.
57+
58+
```kotlin
59+
dependencies {
60+
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
61+
implementation("org.springframework.boot:spring-boot-starter-web")
62+
implementation(project(":aws-advanced-jdbc-wrapper"))
63+
implementation("org.postgresql:postgresql:42.5.4")
64+
}
65+
```
66+
67+
## Step 3: Configure the Datasource
68+
69+
In the `application.yml` file, configure Hikari and AWS Advanced JDBC Wrapper Driver as its driver.
70+
71+
```yaml
72+
spring:
73+
datasource:
74+
url: jdbc:aws-wrapper:postgresql://db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com:5432/database-name
75+
username: some_username
76+
password: some_password
77+
driver-class-name: software.amazon.jdbc.Driver
78+
hikari:
79+
data-source-properties:
80+
wrapperPlugins: failover,efm
81+
wrapperDialect: aurora-pg
82+
exception-override-class-name: software.amazon.jdbc.util.HikariCPSQLException
83+
```
84+
Note that in Spring Boot 2 and 3, Hikari is the default DataSource implementation. So, a bean explicitly specifying Hikari as a Datasource is not needed.
85+
86+
Optionally, you may like to add in Hikari specific configurations like the following.
87+
```yaml
88+
spring:
89+
datasource:
90+
url: jdbc:aws-wrapper:postgresql://database-endpoint-url:5432/database-name
91+
username: some_username
92+
password: some_password
93+
driver-class-name: software.amazon.jdbc.Driver
94+
hikari:
95+
data-source-properties:
96+
wrapperPlugins: failover,efm
97+
wrapperDialect: aurora-pg
98+
exception-override-class-name: software.amazon.jdbc.util.HikariCPSQLException
99+
max-lifetime: 840000
100+
minimum-idle: 10
101+
maximum-pool-size: 20
102+
idle-timeout: 900000
103+
read-only: true
104+
```
105+
106+
107+
## Step 4: Use JDBC Template
108+
109+
Create a new `ApiController` class like the following:
110+
111+
```java
112+
package software.amazon.SpringBootHikariExample;
113+
114+
import org.springframework.beans.factory.annotation.Autowired;
115+
import org.springframework.jdbc.core.JdbcTemplate;
116+
import org.springframework.web.bind.annotation.GetMapping;
117+
import org.springframework.web.bind.annotation.RestController;
118+
119+
@RestController
120+
public class ApiController {
121+
122+
@Autowired
123+
private JdbcTemplate jdbcTemplate;
124+
125+
@GetMapping(value = "/select1")
126+
public Integer getOne() {
127+
return jdbcTemplate.queryForObject("SELECT 1;", Integer.class);
128+
}
129+
}
130+
```
131+
132+
The `@RestController` annotation on the class will allow methods in it to use annotations for mapping HTTP requests.
133+
In this example, the `getOne()` method is annotated with `@GetMapping(value = "/select1")` which will route requests with the path `/select` to that method.
134+
Within the `getOne()` method, the `JdbcTemplate` is called to execute the query `SELECT 1;` and return its results.
135+
136+
## Step 5: Run and call the application
137+
138+
Start the application by running `./gradlew run` in the terminal.
139+
140+
Create an HTTP request to the application by running the following terminal command `curl http://localhost:8080/select1`.
141+
This will trigger the query statement `SELECT 1;` and return the results.
142+
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+
*:eq
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.0"
20+
}
21+
22+
dependencies {
23+
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
24+
implementation("org.springframework.boot:spring-boot-starter-web")
25+
implementation("org.postgresql:postgresql:42.5.4")
26+
implementation(project(":aws-advanced-jdbc-wrapper"))
27+
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
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
package software.amazon.SpringBootHikariExample;
18+
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.jdbc.core.JdbcTemplate;
21+
import org.springframework.web.bind.annotation.GetMapping;
22+
import org.springframework.web.bind.annotation.RestController;
23+
24+
@RestController
25+
public class ApiController {
26+
27+
@Autowired
28+
private JdbcTemplate jdbcTemplate;
29+
30+
@GetMapping(value = "/select1")
31+
public Integer getOne() {
32+
return jdbcTemplate.queryForObject("SELECT 1;", Integer.class);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
package software.amazon.SpringBootHikariExample;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
22+
@SpringBootApplication
23+
public class SpringBootHikariExampleApplication {
24+
public static void main(String[] args) {
25+
SpringApplication.run(SpringBootHikariExampleApplication.class, args);
26+
}
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
spring:
2+
datasource:
3+
url: jdbc:aws-wrapper:postgresql://db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com:5432/postgres
4+
username: username
5+
password: password
6+
driver-class-name: software.amazon.jdbc.Driver
7+
hikari:
8+
data-source-properties:
9+
wrapperPlugins: failover,efm
10+
wrapperDialect: aurora-pg
11+
max-lifetime: 840000
12+
minimum-idle: 20
13+
maximum-pool-size: 20
14+
idle-timeout: 900000
15+
read-only: true

examples/SpringHibernateExample/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Configure Spring to use the AWS Advanced JDBC Driver as the default datasource.
158158

159159
This example contains some very simple configurations for the IAM Authentication plugin, if you are interested in other configurations related to failover, please visit [the documentation for failover parameters](../../docs/using-the-jdbc-driver/using-plugins/UsingTheFailoverPlugin.md#failover-parameters)
160160
2. Configure Hibernate dialect:
161-
```properties
161+
```yaml
162162
jpa:
163163
properties:
164164
hibernate:

examples/SpringWildflyExample/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Create a Gradle project with the following project hierarchy:
4444
├───amazon
4545
│ └───standalone.xml
4646
```
47-
> Note: The wildfly directory will contain all the files you have downloaded in step 3. For simplicity, the diagram above only shows the files requiring modifications or need to be added.
47+
> Note: The wildfly directory will contain all the files that you will download in step 3. For simplicity, the diagram above only shows the files that either need to be added or require modifications.
4848
4949
The file `Example.java` contains the following:
5050
```java

settings.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ include(
2323
"hikari",
2424
"driverexample",
2525
"springhibernate",
26-
"springwildfly"
26+
"springwildfly",
27+
"springboothikariexample"
2728
)
2829

2930
project(":aws-advanced-jdbc-wrapper").projectDir = file("wrapper")
@@ -32,6 +33,7 @@ project(":hikari").projectDir = file("examples/HikariExample")
3233
project(":driverexample").projectDir = file("examples/AWSDriverExample")
3334
project(":springhibernate").projectDir = file("examples/SpringHibernateExample")
3435
project(":springwildfly").projectDir = file("examples/SpringWildflyExample/spring")
36+
project(":springboothikariexample").projectDir = file("examples/SpringBootHikariExample")
3537

3638
pluginManagement {
3739
plugins {

0 commit comments

Comments
 (0)