Skip to content

Commit

Permalink
chore: update deprecated ConnectionProviderManager function calls (#1256
Browse files Browse the repository at this point in the history
)
  • Loading branch information
aaron-congo authored Jan 24, 2025
1 parent 8a812e0 commit a7dd924
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import software.amazon.jdbc.ConnectionPluginManager;
import software.amazon.jdbc.ConnectionProvider;
import software.amazon.jdbc.ConnectionProviderManager;
import software.amazon.jdbc.Driver;
import software.amazon.jdbc.HikariPooledConnectionProvider;
import software.amazon.jdbc.HostListProviderService;
import software.amazon.jdbc.HostSpec;
Expand All @@ -60,12 +61,12 @@
import software.amazon.jdbc.benchmarks.testplugin.TestConnectionWrapper;
import software.amazon.jdbc.dialect.Dialect;
import software.amazon.jdbc.hostavailability.SimpleHostAvailabilityStrategy;
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;
import software.amazon.jdbc.util.telemetry.GaugeCallable;
import software.amazon.jdbc.util.telemetry.TelemetryContext;
import software.amazon.jdbc.util.telemetry.TelemetryCounter;
import software.amazon.jdbc.util.telemetry.TelemetryFactory;
import software.amazon.jdbc.util.telemetry.TelemetryGauge;
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;
import software.amazon.jdbc.wrapper.ConnectionWrapper;

@State(Scope.Benchmark)
Expand Down Expand Up @@ -234,7 +235,7 @@ public ConnectionWrapper initAndReleaseWithAuroraHostListAndReadWriteSplittingPl
public ConnectionWrapper initAndReleaseWithReadWriteSplittingPlugin_internalConnectionPools() throws SQLException {
HikariPooledConnectionProvider provider =
new HikariPooledConnectionProvider((hostSpec, props) -> new HikariConfig());
ConnectionProviderManager.setConnectionProvider(provider);
Driver.setCustomConnectionProvider(provider);
try (ConnectionWrapper wrapper = new TestConnectionWrapper(
useReadWriteSplittingPlugin(),
CONNECTION_STRING,
Expand All @@ -245,7 +246,7 @@ public ConnectionWrapper initAndReleaseWithReadWriteSplittingPlugin_internalConn
mockPluginManagerService)) {
wrapper.releaseResources();
ConnectionProviderManager.releaseResources();
ConnectionProviderManager.resetProvider();
Driver.resetCustomConnectionProvider();
return wrapper;
}
}
Expand All @@ -255,7 +256,7 @@ public ConnectionWrapper initAndReleaseWithAuroraHostListAndReadWriteSplittingPl
throws SQLException {
HikariPooledConnectionProvider provider =
new HikariPooledConnectionProvider((hostSpec, props) -> new HikariConfig());
ConnectionProviderManager.setConnectionProvider(provider);
Driver.setCustomConnectionProvider(provider);
try (ConnectionWrapper wrapper = new TestConnectionWrapper(
useAuroraHostListAndReadWriteSplittingPlugin(),
PG_CONNECTION_STRING,
Expand All @@ -266,7 +267,7 @@ public ConnectionWrapper initAndReleaseWithAuroraHostListAndReadWriteSplittingPl
mockPluginManagerService)) {
wrapper.releaseResources();
ConnectionProviderManager.releaseResources();
ConnectionProviderManager.resetProvider();
Driver.resetCustomConnectionProvider();
return wrapper;
}
}
Expand Down
4 changes: 2 additions & 2 deletions docs/development-guide/Pipelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ For information on how to subscribe to these pipelines, please see the documenta

## Connect Pipeline

The connect pipeline performs any additional setup or post connection steps required to establish a JDBC connection. By default, the connect pipeline will establish connections using the `DriverConnectionProvider` class for Connections requested through the `DriverManager` and `DataSourceConnectionProvider` class for Connections requested through an `AwsWrapperDataSource`. If you would like to use a non-default `ConnectionProvider` to create connections, you can do so by calling `ConnectionProviderManager.setConnectionProvider(ConnectionProvider)`.
The connect pipeline performs any additional setup or post connection steps required to establish a JDBC connection. By default, the connect pipeline will establish connections using the `DriverConnectionProvider` class for Connections requested through the `DriverManager` and `DataSourceConnectionProvider` class for Connections requested through an `AwsWrapperDataSource`. If you would like to use a non-default `ConnectionProvider` to create connections, you can do so by calling `Driver.setCustomConnectionProvider(ConnectionProvider)`.

The most common usage of the connect pipeline is to fetch extra credentials from external locations.

An example would be the IAM connection plugin. The IAM connection plugin generates an IAM authentication token to be used when establishing a connection. Since authentication is only required when establishing a JDBC connection and not required for any subsequent execution, the IAM authentication plugin only needs to implement the connect pipeline.

## Force Connect Pipeline

The force connect pipeline is similar to the connect pipeline except that it will use the default `DriverConnectionProvider` or `DataSourceConnectionProvider` classes to establish connections regardless of whether a non-default `ConnectionProvider` has been requested via `ConnectionProviderManager.setConnectionProvider(ConnectionProvider)`. For most plugins, the connect and force connect implementation will be equivalent.
The force connect pipeline is similar to the connect pipeline except that it will use the default `DriverConnectionProvider` or `DataSourceConnectionProvider` classes to establish connections regardless of whether a non-default `ConnectionProvider` has been requested via `Driver.setCustomConnectionProvider(ConnectionProvider)`. For most plugins, the connect and force connect implementation will be equivalent.

## Execute Pipeline

Expand Down
4 changes: 2 additions & 2 deletions docs/using-the-jdbc-driver/UsingTheJdbcDriver.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ In some use cases you may need to define a specific configuration for a new driv
- you might need to run some initial SQL queries when a connection is established, or;
- you might need to check for some additional conditions to determine the initialization configuration required for a particular connection.

The AWS JDBC Driver allows specifying a special function that can initialize a connection. It can be done with `ConnectionProviderManager.setConnectionInitFunc` method. The `resetConnectionInitFunc` method is also available to remove the function.
The AWS JDBC Driver allows specifying a special function that can initialize a connection. It can be done with `Driver.setConnectionInitFunc` method. The `resetConnectionInitFunc` method is also available to remove the function.

The initialization function is called for all connections, including connections opened by the internal connection pools (see [Using Read Write Splitting Plugin and Internal Connection Pooling](./using-plugins/UsingTheReadWriteSplittingPlugin.md#internal-connection-pooling)). This helps user applications clean up connection sessions that have been altered by previous operations, as returning a connection to a pool will reset the state and retrieving it will call the initialization function again.

> [!WARNING]\
> Executing CPU and network intensive code in the initialization function may significantly impact the AWS JDBC Driver's overall performance.

```java
ConnectionProviderManager.setConnectionInitFunc((connection, protocol, hostSpec, props) -> {
Driver.setConnectionInitFunc((connection, protocol, hostSpec, props) -> {
// Set custom schema for connections to a test-database
if ("test-database".equals(props.getProperty("database"))) {
connection.setSchema("test-database-schema");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final HikariPooledConnectionProvider connProvider =
ReadWriteSplittingPostgresExample::getHikariConfig,
ReadWriteSplittingPostgresExample::getPoolKey
);
ConnectionProviderManager.setConnectionProvider(connProvider);
Driver.setCustomConnectionProvider(connProvider);

private static String getPoolKey(HostSpec hostSpec, Properties props) {
// Include the URL, user, and somePropertyValue in the connection pool key so that a new
Expand All @@ -68,7 +68,7 @@ private static String getPoolKey(HostSpec hostSpec, Properties props) {
}
```

2. Call `ConnectionProviderManager.setConnectionProvider`, passing in the `HikariPooledConnectionProvider` you created in step 1.
2. Call `Driver.setCustomConnectionProvider`, passing in the `HikariPooledConnectionProvider` you created in step 1.

3. By default, the read/write plugin randomly selects a reader instance the first time that `setReadOnly(true)` is called. If you would like the plugin to select a reader based on a different selection strategy, please see the [Reader Selection](#reader-selection) section for more information.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.sql.DriverManager;
import java.sql.SQLException;
import software.amazon.jdbc.ConnectionProviderManager;
import software.amazon.jdbc.Driver;
import software.amazon.jdbc.HikariPooledConnectionProvider;

public class InternalConnectionPoolPasswordWarning {
Expand All @@ -31,7 +32,7 @@ public static void main(String[] args) throws SQLException {
final String correctPassword = "correct_password";
final String wrongPassword = "wrong_password";

ConnectionProviderManager.setConnectionProvider(
Driver.setCustomConnectionProvider(
new HikariPooledConnectionProvider((hostSpec, props) -> new HikariConfig()));

// Create an internal connection pool with the correct password
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void main(String[] args) throws SQLException {
// ReadWriteSplittingMySQLExample::getHikariConfig,
// ReadWriteSplittingMySQLExample::getPoolKey
// );
// ConnectionProviderManager.setConnectionProvider(connProvider);
// Driver.setCustomConnectionProvider(connProvider);

/* Setup Step: Open connection and create tables - uncomment this section to create table and test values */
// try (final Connection connection = DriverManager.getConnection(MYSQL_CONNECTION_STRING, props)) {
Expand Down Expand Up @@ -158,7 +158,7 @@ private static HikariConfig getHikariConfig(HostSpec hostSpec, Properties props)
return config;
}

// This method is an optional parameter to `ConnectionProviderManager.setConnectionProvider`.
// This method is an optional parameter to `Driver.setCustomConnectionProvider`.
// It can be omitted if you do not require it.
private static String getPoolKey(HostSpec hostSpec, Properties props) {
// Include the URL, user, and somePropertyValue in the connection pool key so that a new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static void main(String[] args) throws SQLException {
// ReadWriteSplittingPostgresExample::getHikariConfig,
// ReadWriteSplittingPostgresExample::getPoolKey
// );
// ConnectionProviderManager.setConnectionProvider(connProvider);
// Driver.setCustomConnectionProvider(connProvider);

/* Setup Step: Open connection and create tables - uncomment this section to create table and test values */
// try (final Connection connection = DriverManager.getConnection(POSTGRESQL_CONNECTION_STRING, props)) {
Expand Down Expand Up @@ -159,7 +159,7 @@ private static HikariConfig getHikariConfig(HostSpec hostSpec, Properties props)
return config;
}

// This method is an optional parameter to `ConnectionProviderManager.setConnectionProvider`.
// This method is an optional parameter to `Driver.setCustomConnectionProvider`.
// It can be omitted if you do not require it.
private static String getPoolKey(HostSpec hostSpec, Properties props) {
// Include the URL, user, and somePropertyValue in the connection pool key so that a new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import software.amazon.jdbc.ConnectionProviderManager;
import software.amazon.jdbc.Driver;
import software.amazon.jdbc.HikariPooledConnectionProvider;
import software.amazon.jdbc.HostSpec;
import software.amazon.jdbc.PropertyDefinition;
Expand Down Expand Up @@ -101,7 +102,7 @@ public static void main(String[] args) throws SQLException {
LOGGER.info("Enabling internal connection pools...");
final HikariPooledConnectionProvider provider =
new HikariPooledConnectionProvider(ReadWriteSplittingSample::getHikariConfig);
ConnectionProviderManager.setConnectionProvider(provider);
Driver.setCustomConnectionProvider(provider);
}

final ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <T, E extends Exception> T execute(
/**
* Establishes a connection to the given host using the given driver protocol and properties. If a
* non-default {@link ConnectionProvider} has been set with
* {@link ConnectionProviderManager#setConnectionProvider} and
* {@link Driver#setCustomConnectionProvider(ConnectionProvider)} and
* {@link ConnectionProvider#acceptsUrl(String, HostSpec, Properties)} returns true for the given
* protocol, host, and properties, the connection will be created by the non-default
* ConnectionProvider. Otherwise, the connection will be created by the default
Expand Down Expand Up @@ -77,7 +77,7 @@ Connection connect(
* Establishes a connection to the given host using the given driver protocol and properties. This
* call differs from {@link ConnectionPlugin#connect} in that the default
* {@link ConnectionProvider} will be used to establish the connection even if a non-default
* ConnectionProvider has been set via {@link ConnectionProviderManager#setConnectionProvider}.
* ConnectionProvider has been set via {@link Driver#setCustomConnectionProvider(ConnectionProvider)}.
* The default ConnectionProvider will be {@link DriverConnectionProvider} for connections
* requested via the {@link java.sql.DriverManager} and {@link DataSourceConnectionProvider} for
* connections requested via an {@link software.amazon.jdbc.ds.AwsWrapperDataSource}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public <T, E extends Exception> T execute(
/**
* Establishes a connection to the given host using the given driver protocol and properties. If a
* non-default {@link ConnectionProvider} has been set with
* {@link ConnectionProviderManager#setConnectionProvider} and
* {@link Driver#setCustomConnectionProvider(ConnectionProvider)} and
* {@link ConnectionProvider#acceptsUrl(String, HostSpec, Properties)} returns true for the given
* protocol, host, and properties, the connection will be created by the non-default
* ConnectionProvider. Otherwise, the connection will be created by the default
Expand Down Expand Up @@ -405,7 +405,7 @@ public Connection connect(
* Establishes a connection to the given host using the given driver protocol and properties. This
* call differs from {@link ConnectionPlugin#connect} in that the default
* {@link ConnectionProvider} will be used to establish the connection even if a non-default
* ConnectionProvider has been set via {@link ConnectionProviderManager#setConnectionProvider}.
* ConnectionProvider has been set via {@link Driver#setCustomConnectionProvider(ConnectionProvider)}.
* The default ConnectionProvider will be {@link DriverConnectionProvider} for connections
* requested via the {@link java.sql.DriverManager} and {@link DataSourceConnectionProvider} for
* connections requested via an {@link software.amazon.jdbc.ds.AwsWrapperDataSource}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ConnectionProviderManager(
* {@link ConnectionProvider#acceptsUrl} for more info.
*
* @param connProvider the {@link ConnectionProvider} to use to establish new connections
* @deprecated Use software.amazon.jdbc.Driver instead
* @deprecated Use {@link Driver#setCustomConnectionProvider(ConnectionProvider)} instead.
*/
@Deprecated
public static void setConnectionProvider(ConnectionProvider connProvider) {
Expand Down Expand Up @@ -168,7 +168,7 @@ public HostSpec getHostSpecByStrategy(List<HostSpec> hosts, HostRole role, Strin
* ConnectionProvider will be used if the non-default ConnectionProvider has not been set or has
* been cleared.
*
* @deprecated Use software.amazon.jdbc.Driver instead
* @deprecated Use {@link Driver#resetCustomConnectionProvider()} instead
*/
@Deprecated
public static void resetProvider() {
Expand All @@ -189,7 +189,7 @@ public static void releaseResources() {
* Sets a custom connection initialization function. It'll be used
* for every brand-new database connection.
*
* @deprecated Use software.amazon.jdbc.Driver instead
* @deprecated Use {@link Driver#setConnectionInitFunc(ConnectionInitFunc)} instead
*/
@Deprecated
public static void setConnectionInitFunc(final @NonNull ConnectionInitFunc func) {
Expand All @@ -199,7 +199,7 @@ public static void setConnectionInitFunc(final @NonNull ConnectionInitFunc func)
/**
* Resets a custom connection initialization function.
*
* @deprecated Use software.amazon.jdbc.Driver instead
* @deprecated Use {@link Driver#resetConnectionInitFunc()} instead
*/
@Deprecated
public static void resetConnectionInitFunc() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class HikariPooledConnectionProvider implements PooledConnectionProvider,

/**
* {@link HikariPooledConnectionProvider} constructor. This class can be passed to
* {@link ConnectionProviderManager#setConnectionProvider} to enable internal connection pools for
* {@link Driver#setCustomConnectionProvider(ConnectionProvider)} to enable internal connection pools for
* each database instance in a cluster. By maintaining internal connection pools, the driver can
* improve performance by reusing old {@link Connection} objects.
*
Expand All @@ -99,7 +99,7 @@ public HikariPooledConnectionProvider(HikariPoolConfigurator hikariPoolConfigura

/**
* {@link HikariPooledConnectionProvider} constructor. This class can be passed to
* {@link ConnectionProviderManager#setConnectionProvider} to enable internal connection pools for
* {@link Driver#setCustomConnectionProvider(ConnectionProvider)} to enable internal connection pools for
* each database instance in a cluster. By maintaining internal connection pools, the driver can
* improve performance by reusing old {@link Connection} objects.
*
Expand All @@ -125,7 +125,7 @@ public HikariPooledConnectionProvider(

/**
* {@link HikariPooledConnectionProvider} constructor. This class can be passed to
* {@link ConnectionProviderManager#setConnectionProvider} to enable internal connection pools for
* {@link Driver#setCustomConnectionProvider(ConnectionProvider)} to enable internal connection pools for
* each database instance in a cluster. By maintaining internal connection pools, the driver can
* improve performance by reusing old {@link Connection} objects.
*
Expand Down Expand Up @@ -163,7 +163,7 @@ public HikariPooledConnectionProvider(

/**
* {@link HikariPooledConnectionProvider} constructor. This class can be passed to
* {@link ConnectionProviderManager#setConnectionProvider} to enable internal connection pools for
* {@link Driver#setCustomConnectionProvider(ConnectionProvider)} to enable internal connection pools for
* each database instance in a cluster. By maintaining internal connection pools, the driver can
* improve performance by reusing old {@link Connection} objects.
*
Expand Down
Loading

0 comments on commit a7dd924

Please sign in to comment.