generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 73
docs: sample code for wildfly and hibernate #489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
95c29d0
docs: sample code for wildfly and hibernate
karenc-bq 213b958
chore: downgrade testcontainer mariadb version
karenc-bq 7e558bd
chore: address comments
karenc-bq ad7c70f
chore: bump driver version
karenc-bq aa1bf93
chore: bump driver version
karenc-bq aa36eec
chore: bump driver version
karenc-bq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| # Tutorial: Getting Started with the AWS Advanced JDBC Driver, Spring Boot and Hibernate | ||
|
|
||
| In this tutorial, you will set up a Spring Boot and Hibernate application with the AWS Advanced JDBC Driver, and use the IAM Authentication plugin to fetch some data from an Aurora PostgreSQL database. | ||
|
|
||
| > Note: this tutorial was written using the following technologies: | ||
| > - Spring Boot 2.7.1 | ||
| > - Hibernate | ||
| > - AWS Advanced JDBC Driver 2.2.0 | ||
| > - Postgresql 42.5.4 | ||
| > - Gradle 7 | ||
| > - Java 11 | ||
|
|
||
| You will progress through the following sections: | ||
| 1. Create a Gradle Spring Boot project | ||
| 2. Add the required Gradle dependencies | ||
| 3. Configure the AWS Advanced JDBC Driver | ||
|
|
||
| ## Pre-requisites | ||
| - A database with IAM authentication enabled. This tutorial uses the Aurora PostgreSQL database. For information on how to enable IAM database authentication for Aurora databases, please see the [AWS documentation](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html). | ||
|
|
||
| ## Step 1: Create a Gradle Project | ||
| Create a Gradle project with the following project hierarchy: | ||
|
|
||
| ``` | ||
| └───src | ||
| ├───main | ||
| ├───java | ||
| │ └───example | ||
| | ├───data | ||
| | ├───Example.java | ||
| | └───ExampleRepository.java | ||
| │ └───SpringHibernateExampleApplication.java | ||
| └───resources | ||
| └───application.yml | ||
| ``` | ||
|
|
||
| > Note: this sample code assumes the target database contains a table named Example that can be generated using the SQL queries provided in `src/main/resources/example.sql`. | ||
|
|
||
| `SpringHibernateExampleApplication.java` contains the following the code: | ||
|
|
||
| ```java | ||
| @SpringBootApplication | ||
| public class SpringHibernateExampleApplication implements CommandLineRunner { | ||
| private final Logger LOGGER = LoggerFactory.getLogger(this.getClass()); | ||
|
|
||
| @Autowired | ||
| ExampleRepository repository; | ||
|
|
||
| @Override | ||
| public void run(String... args) { | ||
| LOGGER.info("Example -> {}", repository.findAll()); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| SpringApplication.run(SpringHibernateExampleApplication.class, args); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| `Example.java` contains the following code: | ||
|
|
||
| ```java | ||
| package example.data; | ||
|
|
||
| import javax.persistence.Entity; | ||
| import javax.persistence.GeneratedValue; | ||
| import javax.persistence.Id; | ||
|
|
||
| @Entity | ||
| public class Example { | ||
|
|
||
| @Id | ||
| @GeneratedValue | ||
| private int id; | ||
|
|
||
| private int status; | ||
|
|
||
| public Example() { | ||
| super(); | ||
| } | ||
|
|
||
| public Example(int id, int status) { | ||
| super(); | ||
| this.id = id; | ||
| this.status = status; | ||
| } | ||
|
|
||
| public Example(int status) { | ||
| super(); | ||
| this.status = status; | ||
| } | ||
|
|
||
| public int getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(int id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public int getStatus() { | ||
| return status; | ||
| } | ||
|
|
||
| public void setStatus(int name) { | ||
| this.status = name; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format("Example [id=%s, status=%s]", id, status); | ||
| } | ||
| } | ||
| ``` | ||
| Lastly, `ExampleRepository.java` contains the following code: | ||
|
|
||
| ```java | ||
| package example.data; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface ExampleRepository extends JpaRepository<Example, Integer> { | ||
|
|
||
| } | ||
| ``` | ||
|
|
||
| ## Step 2: Add the required Gradle Dependencies | ||
| In your `build.gradle.kts`, add the following dependencies. | ||
|
|
||
| ``` | ||
| dependencies { | ||
| implementation("org.springframework.boot:spring-boot-starter-jdbc") | ||
| implementation("org.springframework.boot:spring-boot-starter-web") | ||
| implementation("org.postgresql:postgresql") | ||
| implementation("software.amazon.jdbc:aws-advanced-jdbc-wrapper") | ||
| } | ||
| ``` | ||
|
|
||
| ## Step 3: Configure Spring and Hibernate | ||
| Configure Spring to use the AWS Advanced JDBC Driver as the default datasource. | ||
|
|
||
| 1. In the `application.yml`, add a new datasource for Spring: | ||
| ```yaml | ||
| datasource: | ||
| url: jdbc:aws-wrapper:postgresql://db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com:5432/db | ||
| username: jane_doe | ||
| driver-class-name: software.amazon.jdbc.Driver | ||
| hikari: | ||
| data-source-properties: | ||
| wrapperPlugins: iam,failover,efm | ||
| iamRegion: us-east-2 | ||
| iamExpiration: 1320 | ||
| exception-override-class-name: software.amazon.jdbc.util.HikariCPSQLException | ||
| max-lifetime: 1260000 | ||
| ``` | ||
| Since Spring 2+ uses Hikari to manage datasources, to configure the driver we would need to specify the `data-source-properties` under `hikari`. | ||
| Whenever Hikari is used, we also need to ensure failover exceptions are handled correctly so connections will not be discarded from the pool after failover has occurred. This can be done by overriding the exception handling class. For more information on this, please see [the documentation on HikariCP](../../docs/using-the-jdbc-driver/using-plugins/UsingTheFailoverPlugin.md#hikaricp). | ||
|
|
||
| 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) | ||
| 2. Configure Hibernate dialect: | ||
| ```properties | ||
| jpa: | ||
| properties: | ||
| hibernate: | ||
| dialect: org.hibernate.dialect.PostgreSQLDialect | ||
| ``` | ||
| 3. [Optional] You can enable driver logging by adding the following to `application.yml`: | ||
| ```yaml | ||
| logging: | ||
| level: | ||
| software: | ||
| amazon: | ||
| jdbc: TRACE | ||
| ``` | ||
|
|
||
| Start the application by running `./gradlew :springhibernate:bootRun` in the terminal. You should see the application making a connection to the database and fetching data from the Example table. | ||
|
|
||
| # Summary | ||
| This tutorial walks through the steps required to add and configure the AWS Advanced JDBC Driver to a simple Spring Boot and Hibernate application. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| plugins { | ||
| id("org.springframework.boot") version "2.7.0" | ||
| id("io.spring.dependency-management") version "1.1.0" | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation("org.springframework.boot:spring-boot-starter-data-jpa") | ||
| implementation("org.springframework.boot:spring-boot-starter-web") | ||
| implementation("org.postgresql:postgresql:42.5.4") | ||
| implementation("software.amazon.awssdk:rds:2.20.49") | ||
| implementation(project(":aws-advanced-jdbc-wrapper")) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"). | ||
| # You may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # Do not publish the Jar file for this subproject | ||
| nexus.publish=false |
42 changes: 42 additions & 0 deletions
42
examples/SpringHibernateExample/src/main/java/example/SpringHibernateExampleApplication.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package example; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.CommandLineRunner; | ||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| import example.data.ExampleRepository; | ||
|
|
||
| @SpringBootApplication | ||
| public class SpringHibernateExampleApplication implements CommandLineRunner { | ||
| private final Logger LOGGER = LoggerFactory.getLogger(this.getClass()); | ||
|
|
||
| @Autowired | ||
| ExampleRepository repository; | ||
|
|
||
| @Override | ||
| public void run(String... args) { | ||
| LOGGER.info("Example -> {}", repository.findAll()); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| SpringApplication.run(SpringHibernateExampleApplication.class, args); | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
examples/SpringHibernateExample/src/main/java/example/data/Example.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package example.data; | ||
|
|
||
| import javax.persistence.Entity; | ||
| import javax.persistence.GeneratedValue; | ||
| import javax.persistence.Id; | ||
|
|
||
| @Entity | ||
| public class Example { | ||
|
|
||
| @Id | ||
| @GeneratedValue | ||
| private int id; | ||
|
|
||
| private int status; | ||
|
|
||
| public Example() { | ||
| super(); | ||
| } | ||
|
|
||
| public Example(int id, int status) { | ||
| super(); | ||
| this.id = id; | ||
| this.status = status; | ||
| } | ||
|
|
||
| public Example(int status) { | ||
| super(); | ||
| this.status = status; | ||
| } | ||
|
|
||
| public int getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(int id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public int getStatus() { | ||
| return status; | ||
| } | ||
|
|
||
| public void setStatus(int name) { | ||
| this.status = name; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format("Example [id=%s, status=%s]", id, status); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
examples/SpringHibernateExample/src/main/java/example/data/ExampleRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package example.data; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface ExampleRepository extends JpaRepository<Example, Integer> { | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this block labeled a properties block instead of a yaml block?