diff --git a/docs/using-the-jdbc-driver/DataSource.md b/docs/using-the-jdbc-driver/DataSource.md index b5bdc733c..e95be0e45 100644 --- a/docs/using-the-jdbc-driver/DataSource.md +++ b/docs/using-the-jdbc-driver/DataSource.md @@ -66,7 +66,7 @@ To use the AWS JDBC Driver with a connection pool, you must: ds.addDataSourceProperty("database", "postgres"); // Alternatively, the AwsWrapperDataSource can be configured with a JDBC URL instead of individual properties as seen above. - ds.addDataSourceProperty("jdbcUrl", "jdbc:aws-wrapper:postgresql://db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com:5432/postgres"); + ds.addDataSourceProperty("jdbcUrl", "jdbc:aws-wrapper:postgresql://db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com:5432/postgres");// See this [example](../../examples/AWSDriverExample/src/main/java/software/amazon/DatasourceUrlExample.java) ``` 4. Set the driver-specific datasource: @@ -80,8 +80,11 @@ To use the AWS JDBC Driver with a connection pool, you must: targetDataSourceProps.setProperty("socketTimeout", "10"); targetDataSourceProps.setProperty("wrapperLoggerLevel", "ALL"); ds.addDataSourceProperty("targetDataSourceProperties", targetDataSourceProps); - ``` + // Alternatively, the driver-specific datasource and any AWS JDBC Driver properties can be configured with JDBC URL, instead in code: + ds.addDataSourceProperty("jdbcUrl", "jdbc:aws-wrapper:postgresql://db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com:5432/postgres?socketTimeout=10&wrapperLoggerLevel=ALL"); +``` + > [!WARNING]\ > HikariCP supports either DataSource-based configuration or DriverManager-based configuration by specifying the `dataSourceClassName` or the `jdbcUrl`. When using the `AwsWrapperDataSource` you must specify the `dataSourceClassName`, and the `HikariDataSource.setJdbcUrl` method should not be used. For more information see HikariCP's [documentation](https://github.com/brettwooldridge/HikariCP#gear-configuration-knobs-baby). diff --git a/examples/AWSDriverExample/src/main/java/software/amazon/DatasourceUrlExample.java b/examples/AWSDriverExample/src/main/java/software/amazon/DatasourceUrlExample.java new file mode 100644 index 000000000..09c8a2ea0 --- /dev/null +++ b/examples/AWSDriverExample/src/main/java/software/amazon/DatasourceUrlExample.java @@ -0,0 +1,46 @@ +/* + * 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 software.amazon; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import software.amazon.jdbc.ds.AwsWrapperDataSource; + +public class DatasourceUrlExample { + private static final String USER = "username"; + private static final String PASSWORD = "password"; + + public static void main(String[] args) throws SQLException { + AwsWrapperDataSource ds = new AwsWrapperDataSource(); + // Configure basic information and any driver-specific and AWS JDBC Driver properties: + // Configure any AWS JDBC Driver properties: + ds.setJdbcUrl("jdbc:aws-wrapper:postgresql://db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com/employees?ssl=true&wrapperLoggerLevel=ALL"); + + // Specify the driver-specific data source: + ds.setTargetDataSourceClassName("org.postgresql.ds.PGSimpleDataSource"); + + + // Try and make a connection: + try (final Connection conn = ds.getConnection(USER, PASSWORD); + final Statement statement = conn.createStatement(); + final ResultSet rs = statement.executeQuery("SELECT * FROM employees")) { + System.out.println(Util.getResult(rs)); + } + } +} diff --git a/examples/SpringBootHikariExample/README.md b/examples/SpringBootHikariExample/README.md index 7d5190d06..bce9e61fe 100644 --- a/examples/SpringBootHikariExample/README.md +++ b/examples/SpringBootHikariExample/README.md @@ -68,7 +68,7 @@ Please note that the sample code inside the AWS JDBC Driver project will use the ## Step 3: Configure the Datasource -In the `application.yml` file, configure Hikari and AWS JDBC Driver as its driver. +In the `application.yml` file, configure Hikari and AWS JDBC Driver as its driver. ```yaml spring: @@ -84,6 +84,18 @@ spring: exception-override-class-name: software.amazon.jdbc.util.HikariCPSQLException ``` 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. +Note also that data-source-properties can also be configured by parameters of jdbc url: +```yaml +spring: + datasource: + url: jdbc:aws-wrapper:postgresql://db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com:5432/database-name?wrapperPlugins=failover,efm2&wrappeDialect=aurora-pg + username: some_username + password: some_password + driver-class-name: software.amazon.jdbc.Driver + hikari: + exception-override-class-name: software.amazon.jdbc.util.HikariCPSQLException +``` + Optionally, you may like to add in Hikari specific configurations like the following. ```yaml