Skip to content

Commit

Permalink
fix failing HikariCP integration test (#468)
Browse files Browse the repository at this point in the history
Co-authored-by: sergiyv-bitquill <[email protected]>
Co-authored-by: Karen <[email protected]>
  • Loading branch information
3 people authored Jun 3, 2023
1 parent 9be3a6a commit d006fdd
Show file tree
Hide file tree
Showing 34 changed files with 353 additions and 489 deletions.
47 changes: 21 additions & 26 deletions docs/using-the-jdbc-driver/DataSource.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@ To establish a connection with the AwsWrapperDataSource, you must:

### Configurable DataSource Properties

To allow the AWS Advanced JDBC Driver to work with multiple driver-specific datasources,
you need to specify what property names the underlying datasource uses.
For example, one datasource implementation could use the method `setUser` to set the datasource username,
while another might use the method `setUsername` for the same task. See the table below for a list of configurable property names.

> **:warning: Note:** If the same connection property is provided both explicitly in the connection URL and in the datasource properties, the value set in the connection URL will take precedence.
| Property | Configuration Method | Description | Type | Required | Example |
|-----------------------------|--------------------------------|---------------------------------------------------------------------------------------------------|----------|------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------|
| Server name | `setServerPropertyName` | The name of the server or host property. | `String` | Yes, if no URL is provided. | `serverName` |
| Database name | `setDatabasePropertyName` | The name of the database property. | `String` | No | `databaseName` |
| Port name | `setPortPropertyName` | The name of the port property. | `String` | No | `port` |
| URL name | `setUrlPropertyName` | The name of the URL property. | `String` | No, but some drivers, such as MariaDb, require some parameters to be included in the URL so it is recommended to provide this parameter. | `url` |
| JDBC URL | `setJdbcUrl` | The URL to connect with. | `String` | No, if there is enough information provided by the other properties that can be used to create a URL. | `jdbc:postgresql://localhost/postgres` |
| JDBC protocol | `setJdbcProtocol` | The JDBC protocol that will be used. | `String` | Yes, if the JDBC URL has not been set. | `jdbc:postgresql:` |
| Underlying DataSource class | `setTargetDataSourceClassName` | The fully qualified class name of the underlying DataSource class the AWS JDBC Driver should use. | `String` | Yes, if the JDBC URL has not been set. | `org.postgresql.ds.PGSimpleDataSource` |
See the table below for a list of configurable properties.

> **:warning: Note:** If the same connection property is provided both explicitly in the connection URL and in the datasource properties, the value set in the datasource properties will take precedence.
| Property | Configuration Method | Description | Type | Required | Example |
|-----------------------------|--------------------------------|---------------------------------------------------------------------------------------------------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------|
| Server name | `setServerName` | The name of the server. | `String` | Yes, if no URL is provided. | `db-server.mydomain.com` |
| Server port | `setServerPort` | The server port. | `String` | No | `5432` |
| Database name | `setDatabase` | The name of the database. | `String` | No | `testDatabase` |
| JDBC URL | `setJdbcUrl` | The URL to connect with. | `String` | No. Either URL or server name should be set. If both URL and server name have been set, URL will take precedence. Please note that some drivers, such as MariaDb, require some parameters to be included particularly in the URL. | `jdbc:postgresql://localhost/postgres` |
| JDBC protocol | `setJdbcProtocol` | The JDBC protocol that will be used. | `String` | Yes, if the JDBC URL has not been set. | `jdbc:postgresql:` |
| Underlying DataSource class | `setTargetDataSourceClassName` | The fully qualified class name of the underlying DataSource class the AWS JDBC Driver should use. | `String` | Yes, if the JDBC URL has not been set. | `org.postgresql.ds.PGSimpleDataSource` |

## Using the AwsWrapperDataSource with Connection Pooling Frameworks

Expand Down Expand Up @@ -63,21 +59,20 @@ To use the AWS JDBC Driver with a connection pool, you must:
```java
// Note: jdbcProtocol is required when connecting via server name
ds.addDataSourceProperty("jdbcProtocol", "jdbc:postgresql:");
ds.addDataSourceProperty("serverPropertyName", "serverName");

ds.addDataSourceProperty("databasePropertyName", "databaseName");
ds.addDataSourceProperty("portPropertyName", "port");
ds.addDataSourceProperty("serverName", "db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com");
ds.addDataSourceProperty("serverPort", "5432");
ds.addDataSourceProperty("database", "postgres");
```

4. Configure the driver-specific datasource:
4. Set the driver-specific datasource:
```java
ds.addDataSourceProperty("targetDataSourceClassName", "org.postgresql.ds.PGSimpleDataSource");

```

5. Configure the driver-specific datasource, if needed. This step is optional:
```java
Properties targetDataSourceProps = new Properties();
targetDataSourceProps.setProperty("serverName", "db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com");
targetDataSourceProps.setProperty("databaseName", "postgres");
targetDataSourceProps.setProperty("port", "5432");

targetDataSourceProps.setProperty("socketTimeout", "10");
ds.addDataSourceProperty("targetDataSourceProperties", targetDataSourceProps);
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,15 @@ public static void main(String[] args) throws SQLException {

// Configure the property names for the underlying driver-specific data source:
ds.setJdbcProtocol("jdbc:postgresql:");
ds.setDatabasePropertyName("databaseName");
ds.setServerPropertyName("serverName");
ds.setPortPropertyName("port");

// Specify the driver-specific data source:
ds.setTargetDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");

// Configure the driver-specific data source:
Properties targetDataSourceProps = new Properties();
targetDataSourceProps.setProperty("serverName", "db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com");
targetDataSourceProps.setProperty("databaseName", "employees");
targetDataSourceProps.setProperty("port", "5432");
targetDataSourceProps.setProperty("database", "employees");
targetDataSourceProps.setProperty("serverPort", "5432");
ds.setTargetDataSourceProperties(targetDataSourceProps);

// Try and make a connection:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,15 @@ private static void scenario3() throws SQLException {
private static DataSource getSimplePostgresDataSource() {
AwsWrapperDataSource ds = new AwsWrapperDataSource();
ds.setJdbcProtocol("jdbc:postgresql:");
ds.setDatabasePropertyName("databaseName");
ds.setServerPropertyName("serverName");
ds.setPortPropertyName("port");
ds.setServerName(DATABASE_URL);
ds.setDatabase(DATABASE_NAME);
ds.setServerPort("5432");

ds.setTargetDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");

Properties targetDataSourceProps = new Properties();
targetDataSourceProps.setProperty(
PropertyDefinition.PLUGINS.name, "readWriteSplitting,failover,efm");
targetDataSourceProps.setProperty("serverName", DATABASE_URL);
targetDataSourceProps.setProperty("databaseName", DATABASE_NAME);
targetDataSourceProps.setProperty("port", "5432");

ds.setUser(USERNAME);
ds.setPassword(PASSWORD);
Expand All @@ -112,19 +109,15 @@ private static DataSource getHikariCPDataSource() {

ds.setDataSourceClassName(AwsWrapperDataSource.class.getName());
ds.addDataSourceProperty("jdbcProtocol", "jdbc:postgresql:");
ds.addDataSourceProperty("serverPropertyName", "serverName");

ds.addDataSourceProperty("databasePropertyName", "databaseName");
ds.addDataSourceProperty("portPropertyName", "port");
ds.addDataSourceProperty("serverName", DATABASE_URL);
ds.addDataSourceProperty("serverPort", "5432");
ds.addDataSourceProperty("database", DATABASE_NAME);

ds.addDataSourceProperty("targetDataSourceClassName", "org.postgresql.ds.PGSimpleDataSource");

Properties targetDataSourceProps = new Properties();
targetDataSourceProps.setProperty(PropertyDefinition.PLUGINS.name,
"readWriteSplitting,failover,efm");
targetDataSourceProps.setProperty("serverName", DATABASE_URL);
targetDataSourceProps.setProperty("databaseName", DATABASE_NAME);
targetDataSourceProps.setProperty("port", "5432");

ds.addDataSourceProperty("targetDataSourceProperties", targetDataSourceProps);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,17 @@ public static void main(String[] args) throws SQLException {

// Configure AwsWrapperDataSource:
ds.addDataSourceProperty("jdbcProtocol", "jdbc:postgresql:");
ds.addDataSourceProperty("databasePropertyName", "databaseName");
ds.addDataSourceProperty("portPropertyName", "portNumber");
ds.addDataSourceProperty("serverPropertyName", "serverName");
ds.addDataSourceProperty("database", DATABASE_NAME);
ds.addDataSourceProperty("serverPort", "5432");
ds.addDataSourceProperty("serverName", ENDPOINT);

// Specify the driver-specific data source for AwsWrapperDataSource:
ds.addDataSourceProperty("targetDataSourceClassName", "org.postgresql.ds.PGSimpleDataSource");

// Configuring PGSimpleDataSource:
Properties targetDataSourceProps = new Properties();
targetDataSourceProps.setProperty("serverName", ENDPOINT);
targetDataSourceProps.setProperty("databaseName", DATABASE_NAME);
targetDataSourceProps.setProperty("portNumber", "5432");

ds.addDataSourceProperty("targetDataSourceProperties", targetDataSourceProps);
// Configuring PGSimpleDataSource (optional):
// Properties targetDataSourceProps = new Properties();
// targetDataSourceProps.setProperty("socketTimeout", "10");
// ds.addDataSourceProperty("targetDataSourceProperties", targetDataSourceProps);

// Attempt a connection:
try (final Connection conn = ds.getConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public static void main(String[] args) throws SQLException {

// Configure AwsWrapperDataSource:
ds.addDataSourceProperty("jdbcProtocol", "jdbc:postgresql:");
ds.addDataSourceProperty("databasePropertyName", "databaseName");
ds.addDataSourceProperty("portPropertyName", "portNumber");
ds.addDataSourceProperty("serverPropertyName", "serverName");
ds.addDataSourceProperty("serverName", ENDPOINT);
ds.addDataSourceProperty("portNumber", "5432");
ds.addDataSourceProperty("database", DATABASE_NAME);

// The failover plugin throws failover-related exceptions that need to be handled explicitly by HikariCP,
// otherwise connections will be closed immediately after failover. Set `ExceptionOverrideClassName` to provide
Expand All @@ -58,11 +58,10 @@ public static void main(String[] args) throws SQLException {
// Specify the driver-specific data source for AwsWrapperDataSource:
ds.addDataSourceProperty("targetDataSourceClassName", "org.postgresql.ds.PGSimpleDataSource");

// Configuring PGSimpleDataSource:
Properties targetDataSourceProps = new Properties();
targetDataSourceProps.setProperty("serverName", ENDPOINT);
targetDataSourceProps.setProperty("databaseName", DATABASE_NAME);
targetDataSourceProps.setProperty("portNumber", "5432");

// Configuring PGSimpleDataSource if needed:
// targetDataSourceProps.setProperty("ssl", "true");

// Enable the failover and host monitoring connection plugins.
targetDataSourceProps.setProperty("wrapperPlugins", "failover,efm");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package software.amazon.jdbc;

import static software.amazon.jdbc.util.StringUtils.isNullOrEmpty;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
Expand All @@ -29,7 +27,6 @@
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import software.amazon.jdbc.dialect.Dialect;
import software.amazon.jdbc.exceptions.SQLLoginException;
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;
Expand All @@ -53,28 +50,16 @@ public class DataSourceConnectionProvider implements ConnectionProvider {
}
});
private final @NonNull DataSource dataSource;
private final @Nullable String serverPropertyName;
private final @Nullable String portPropertyName;
private final @Nullable String urlPropertyName;
private final @Nullable String databasePropertyName;
private final @NonNull TargetDriverDialect targetDriverDialect;


private final ReentrantLock lock = new ReentrantLock();

public DataSourceConnectionProvider(
final @NonNull DataSource dataSource,
final @NonNull TargetDriverDialect targetDriverDialect,
final @Nullable String serverPropertyName,
final @Nullable String portPropertyName,
final @Nullable String urlPropertyName,
final @Nullable String databasePropertyName) {
final @NonNull TargetDriverDialect targetDriverDialect) {
this.dataSource = dataSource;
this.targetDriverDialect = targetDriverDialect;
this.serverPropertyName = serverPropertyName;
this.portPropertyName = portPropertyName;
this.urlPropertyName = urlPropertyName;
this.databasePropertyName = databasePropertyName;
}

/**
Expand Down Expand Up @@ -143,11 +128,7 @@ public Connection connect(
ds,
protocol,
hostSpec,
copy,
this.serverPropertyName,
this.portPropertyName,
this.urlPropertyName,
this.databasePropertyName);
copy);
conn = ds.getConnection();

} else {
Expand All @@ -161,11 +142,7 @@ public Connection connect(
this.dataSource,
protocol,
hostSpec,
copy,
this.serverPropertyName,
this.portPropertyName,
this.urlPropertyName,
this.databasePropertyName);
copy);
conn = this.dataSource.getConnection();
} finally {
this.lock.unlock();
Expand All @@ -191,16 +168,7 @@ public Connection connect(
@Deprecated
public Connection connect(final @NonNull String url, final @NonNull Properties props) throws SQLException {
final Properties copy = PropertyUtils.copyProperties(props);

if (!isNullOrEmpty(this.urlPropertyName)) {
copy.setProperty(this.urlPropertyName, url);
}

if (!isNullOrEmpty(this.databasePropertyName)
&& !isNullOrEmpty(PropertyDefinition.DATABASE.getString(props))) {
copy.put(this.databasePropertyName, PropertyDefinition.DATABASE.getString(props));
}

copy.setProperty("url", url);
PropertyDefinition.removeAllExceptCredentials(copy);
PropertyUtils.applyProperties(this.dataSource, copy);
return this.dataSource.getConnection();
Expand Down
Loading

0 comments on commit d006fdd

Please sign in to comment.