|
| 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 | + |
0 commit comments