Skip to content

Commit 2e6ae7f

Browse files
authoredJan 10, 2025··
Relocate custom handlers (#1235)
1 parent 115dfda commit 2e6ae7f

File tree

5 files changed

+185
-43
lines changed

5 files changed

+185
-43
lines changed
 

‎wrapper/src/main/java/software/amazon/jdbc/ConnectionProviderManager.java

+36-23
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,15 @@
2020
import java.sql.SQLException;
2121
import java.util.List;
2222
import java.util.Properties;
23-
import java.util.concurrent.atomic.AtomicReference;
2423
import org.checkerframework.checker.nullness.qual.NonNull;
2524
import org.checkerframework.checker.nullness.qual.Nullable;
2625
import software.amazon.jdbc.cleanup.CanReleaseResources;
2726

2827
public class ConnectionProviderManager {
2928

30-
private static AtomicReference<ConnectionProvider> customConnectionProvider = new AtomicReference<>(null);
31-
3229
private final ConnectionProvider defaultProvider;
3330
private final @Nullable ConnectionProvider effectiveConnProvider;
3431

35-
private static ConnectionInitFunc connectionInitFunc = null;
36-
3732
/**
3833
* {@link ConnectionProviderManager} constructor.
3934
*
@@ -57,9 +52,11 @@ public ConnectionProviderManager(
5752
* {@link ConnectionProvider#acceptsUrl} for more info.
5853
*
5954
* @param connProvider the {@link ConnectionProvider} to use to establish new connections
55+
* @deprecated Use software.amazon.jdbc.Driver instead
6056
*/
57+
@Deprecated
6158
public static void setConnectionProvider(ConnectionProvider connProvider) {
62-
customConnectionProvider.set(connProvider);
59+
Driver.setCustomConnectionProvider(connProvider);
6360
}
6461

6562
/**
@@ -78,9 +75,9 @@ public static void setConnectionProvider(ConnectionProvider connProvider) {
7875
public ConnectionProvider getConnectionProvider(
7976
String driverProtocol, HostSpec host, Properties props) {
8077

81-
final ConnectionProvider tmpCustomConnectionProvider = customConnectionProvider.get();
82-
if (tmpCustomConnectionProvider != null && tmpCustomConnectionProvider.acceptsUrl(driverProtocol, host, props)) {
83-
return tmpCustomConnectionProvider;
78+
final ConnectionProvider customConnectionProvider = Driver.getCustomConnectionProvider();
79+
if (customConnectionProvider != null && customConnectionProvider.acceptsUrl(driverProtocol, host, props)) {
80+
return customConnectionProvider;
8481
}
8582

8683
if (this.effectiveConnProvider != null && this.effectiveConnProvider.acceptsUrl(driverProtocol, host, props)) {
@@ -110,8 +107,8 @@ public ConnectionProvider getDefaultProvider() {
110107
* return false
111108
*/
112109
public boolean acceptsStrategy(HostRole role, String strategy) {
113-
final ConnectionProvider tmpCustomConnectionProvider = customConnectionProvider.get();
114-
if (tmpCustomConnectionProvider != null && tmpCustomConnectionProvider.acceptsStrategy(role, strategy)) {
110+
final ConnectionProvider customConnectionProvider = Driver.getCustomConnectionProvider();
111+
if (customConnectionProvider != null && customConnectionProvider.acceptsStrategy(role, strategy)) {
115112
return true;
116113
}
117114

@@ -143,10 +140,10 @@ public boolean acceptsStrategy(HostRole role, String strategy) {
143140
public HostSpec getHostSpecByStrategy(List<HostSpec> hosts, HostRole role, String strategy, Properties props)
144141
throws SQLException, UnsupportedOperationException {
145142
HostSpec host = null;
146-
final ConnectionProvider tmpCustomConnectionProvider = customConnectionProvider.get();
143+
final ConnectionProvider customConnectionProvider = Driver.getCustomConnectionProvider();
147144
try {
148-
if (tmpCustomConnectionProvider != null && tmpCustomConnectionProvider.acceptsStrategy(role, strategy)) {
149-
host = tmpCustomConnectionProvider.getHostSpecByStrategy(hosts, role, strategy, props);
145+
if (customConnectionProvider != null && customConnectionProvider.acceptsStrategy(role, strategy)) {
146+
host = customConnectionProvider.getHostSpecByStrategy(hosts, role, strategy, props);
150147
}
151148
} catch (UnsupportedOperationException e) {
152149
// The custom provider does not support the provided strategy, ignore it and try with the other providers.
@@ -170,27 +167,43 @@ public HostSpec getHostSpecByStrategy(List<HostSpec> hosts, HostRole role, Strin
170167
* Clears the non-default {@link ConnectionProvider} if it has been set. The default
171168
* ConnectionProvider will be used if the non-default ConnectionProvider has not been set or has
172169
* been cleared.
170+
*
171+
* @deprecated Use software.amazon.jdbc.Driver instead
173172
*/
173+
@Deprecated
174174
public static void resetProvider() {
175-
customConnectionProvider.set(null);
175+
Driver.resetCustomConnectionProvider();
176176
}
177177

178178
/**
179179
* Releases any resources held by the available {@link ConnectionProvider} instances.
180180
*/
181181
public static void releaseResources() {
182-
final ConnectionProvider tmpCustomConnectionProvider = customConnectionProvider.get();
183-
if (tmpCustomConnectionProvider instanceof CanReleaseResources) {
184-
((CanReleaseResources) tmpCustomConnectionProvider).releaseResources();
182+
final ConnectionProvider customConnectionProvider = Driver.getCustomConnectionProvider();
183+
if (customConnectionProvider instanceof CanReleaseResources) {
184+
((CanReleaseResources) customConnectionProvider).releaseResources();
185185
}
186186
}
187187

188+
/**
189+
* Sets a custom connection initialization function. It'll be used
190+
* for every brand-new database connection.
191+
*
192+
* @deprecated Use software.amazon.jdbc.Driver instead
193+
*/
194+
@Deprecated
188195
public static void setConnectionInitFunc(final @NonNull ConnectionInitFunc func) {
189-
connectionInitFunc = func;
196+
Driver.setConnectionInitFunc(func);
190197
}
191198

199+
/**
200+
* Resets a custom connection initialization function.
201+
*
202+
* @deprecated Use software.amazon.jdbc.Driver instead
203+
*/
204+
@Deprecated
192205
public static void resetConnectionInitFunc() {
193-
connectionInitFunc = null;
206+
Driver.resetConnectionInitFunc();
194207
}
195208

196209
public void initConnection(
@@ -199,12 +212,12 @@ public void initConnection(
199212
final @NonNull HostSpec hostSpec,
200213
final @NonNull Properties props) throws SQLException {
201214

202-
final ConnectionInitFunc copy = connectionInitFunc;
203-
if (copy == null) {
215+
final ConnectionInitFunc connectionInitFunc = Driver.getConnectionInitFunc();
216+
if (connectionInitFunc == null) {
204217
return;
205218
}
206219

207-
copy.initConnection(connection, protocol, hostSpec, props);
220+
connectionInitFunc.initConnection(connection, protocol, hostSpec, props);
208221
}
209222

210223
public interface ConnectionInitFunc {

‎wrapper/src/main/java/software/amazon/jdbc/Driver.java

+102-8
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@
2525
import java.util.Collections;
2626
import java.util.List;
2727
import java.util.Properties;
28+
import java.util.concurrent.atomic.AtomicReference;
2829
import java.util.function.Function;
2930
import java.util.logging.ConsoleHandler;
3031
import java.util.logging.Handler;
3132
import java.util.logging.Level;
3233
import java.util.logging.Logger;
3334
import org.checkerframework.checker.nullness.qual.NonNull;
3435
import org.checkerframework.checker.nullness.qual.Nullable;
36+
import software.amazon.jdbc.ConnectionProviderManager.ConnectionInitFunc;
3537
import software.amazon.jdbc.authentication.AwsCredentialsManager;
38+
import software.amazon.jdbc.dialect.Dialect;
3639
import software.amazon.jdbc.dialect.DialectManager;
40+
import software.amazon.jdbc.exceptions.ExceptionHandler;
3741
import software.amazon.jdbc.hostlistprovider.RdsHostListProvider;
3842
import software.amazon.jdbc.hostlistprovider.monitoring.MonitoringRdsHostListProvider;
3943
import software.amazon.jdbc.plugin.AwsSecretsManagerCacheHolder;
@@ -73,8 +77,25 @@ public class Driver implements java.sql.Driver {
7377
private static final Logger LOGGER = Logger.getLogger("software.amazon.jdbc.Driver");
7478
private static @Nullable Driver registeredDriver;
7579

76-
private static ResetSessionStateOnCloseCallable resetSessionStateOnCloseCallable = null;
77-
private static TransferSessionStateOnSwitchCallable transferSessionStateOnSwitchCallable = null;
80+
private static final AtomicReference<ResetSessionStateOnCloseCallable> resetSessionStateOnCloseCallable =
81+
new AtomicReference<>(null);
82+
private static final AtomicReference<TransferSessionStateOnSwitchCallable> transferSessionStateOnSwitchCallable =
83+
new AtomicReference<>(null);
84+
85+
private static final AtomicReference<ExceptionHandler> customExceptionHandler =
86+
new AtomicReference<>(null);
87+
88+
private static final AtomicReference<Dialect> customDialect =
89+
new AtomicReference<>(null);
90+
91+
private static final AtomicReference<TargetDriverDialect> customTargetDriverDialect =
92+
new AtomicReference<>(null);
93+
94+
private static final AtomicReference<ConnectionProvider> customConnectionProvider =
95+
new AtomicReference<>(null);
96+
97+
private static final AtomicReference<ConnectionInitFunc> connectionInitFunc =
98+
new AtomicReference<>(null);
7899

79100
static {
80101
try {
@@ -262,27 +283,27 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
262283
}
263284

264285
public static void setResetSessionStateOnCloseFunc(final @NonNull ResetSessionStateOnCloseCallable func) {
265-
resetSessionStateOnCloseCallable = func;
286+
resetSessionStateOnCloseCallable.set(func);
266287
}
267288

268289
public static void resetResetSessionStateOnCloseFunc() {
269-
resetSessionStateOnCloseCallable = null;
290+
resetSessionStateOnCloseCallable.set(null);
270291
}
271292

272293
public static ResetSessionStateOnCloseCallable getResetSessionStateOnCloseFunc() {
273-
return resetSessionStateOnCloseCallable;
294+
return resetSessionStateOnCloseCallable.get();
274295
}
275296

276297
public static void setTransferSessionStateOnSwitchFunc(final @NonNull TransferSessionStateOnSwitchCallable func) {
277-
transferSessionStateOnSwitchCallable = func;
298+
transferSessionStateOnSwitchCallable.set(func);
278299
}
279300

280301
public static void resetTransferSessionStateOnSwitchFunc() {
281-
transferSessionStateOnSwitchCallable = null;
302+
transferSessionStateOnSwitchCallable.set(null);
282303
}
283304

284305
public static TransferSessionStateOnSwitchCallable getTransferSessionStateOnSwitchFunc() {
285-
return transferSessionStateOnSwitchCallable;
306+
return transferSessionStateOnSwitchCallable.get();
286307
}
287308

288309
public static void setPrepareHostFunc(final Function<String, String> func) {
@@ -293,6 +314,79 @@ public static void resetPrepareHostFunc() {
293314
RdsUtils.resetPrepareHostFunc();
294315
}
295316

317+
public static void setCustomExceptionHandler(final ExceptionHandler exceptionHandler) {
318+
customExceptionHandler.set(exceptionHandler);
319+
}
320+
321+
public static ExceptionHandler getCustomExceptionHandler() {
322+
return customExceptionHandler.get();
323+
}
324+
325+
public static void resetCustomExceptionHandler() {
326+
customExceptionHandler.set(null);
327+
}
328+
329+
public static void setCustomDialect(final @NonNull Dialect dialect) {
330+
customDialect.set(dialect);
331+
}
332+
333+
public static Dialect getCustomDialect() {
334+
return customDialect.get();
335+
}
336+
337+
public static void resetCustomDialect() {
338+
customDialect.set(null);
339+
}
340+
341+
public static void setCustomTargetDriverDialect(final @NonNull TargetDriverDialect targetDriverDialect) {
342+
customTargetDriverDialect.set(targetDriverDialect);
343+
}
344+
345+
public static TargetDriverDialect getCustomTargetDriverDialect() {
346+
return customTargetDriverDialect.get();
347+
}
348+
349+
public static void resetCustomTargetDriverDialect() {
350+
customTargetDriverDialect.set(null);
351+
}
352+
353+
/**
354+
* Setter that can optionally be called to request a non-default {@link ConnectionProvider}. The
355+
* requested ConnectionProvider will be used to establish future connections unless it does not
356+
* support a requested URL, in which case the default ConnectionProvider will be used. See
357+
* {@link ConnectionProvider#acceptsUrl} for more info.
358+
*
359+
* @param connProvider the {@link ConnectionProvider} to use to establish new connections
360+
*/
361+
public static void setCustomConnectionProvider(ConnectionProvider connProvider) {
362+
customConnectionProvider.set(connProvider);
363+
}
364+
365+
public static ConnectionProvider getCustomConnectionProvider() {
366+
return customConnectionProvider.get();
367+
}
368+
369+
/**
370+
* Clears the non-default {@link ConnectionProvider} if it has been set. The default
371+
* ConnectionProvider will be used if the non-default ConnectionProvider has not been set or has
372+
* been cleared.
373+
*/
374+
public static void resetCustomConnectionProvider() {
375+
customConnectionProvider.set(null);
376+
}
377+
378+
public static void setConnectionInitFunc(final @NonNull ConnectionInitFunc func) {
379+
connectionInitFunc.set(func);
380+
}
381+
382+
public static ConnectionInitFunc getConnectionInitFunc() {
383+
return connectionInitFunc.get();
384+
}
385+
386+
public static void resetConnectionInitFunc() {
387+
connectionInitFunc.set(null);
388+
}
389+
296390
public static void clearCaches() {
297391
RdsUtils.clearCache();
298392
RdsHostListProvider.clearAll();

‎wrapper/src/main/java/software/amazon/jdbc/dialect/DialectManager.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.logging.Logger;
2727
import org.checkerframework.checker.nullness.qual.NonNull;
2828
import software.amazon.jdbc.AwsWrapperProperty;
29+
import software.amazon.jdbc.Driver;
2930
import software.amazon.jdbc.HostSpec;
3031
import software.amazon.jdbc.PluginService;
3132
import software.amazon.jdbc.util.CacheMap;
@@ -40,8 +41,6 @@ public class DialectManager implements DialectProvider {
4041

4142
private static final Logger LOGGER = Logger.getLogger(DialectManager.class.getName());
4243

43-
protected static Dialect customDialect;
44-
4544
public static final AwsWrapperProperty DIALECT = new AwsWrapperProperty(
4645
"wrapperDialect", "",
4746
"A unique identifier for the supported database dialect.");
@@ -88,12 +87,24 @@ public DialectManager(PluginService pluginService) {
8887
this.pluginService = pluginService;
8988
}
9089

90+
/**
91+
* Sets a custom dialect handler.
92+
*
93+
* @deprecated Use software.amazon.jdbc.Driver instead
94+
*/
95+
@Deprecated
9196
public static void setCustomDialect(final @NonNull Dialect dialect) {
92-
customDialect = dialect;
97+
Driver.setCustomDialect(dialect);
9398
}
9499

100+
/**
101+
* Resets a custom dialect handler.
102+
*
103+
* @deprecated Use software.amazon.jdbc.Driver instead
104+
*/
105+
@Deprecated
95106
public static void resetCustomDialect() {
96-
customDialect = null;
107+
Driver.resetCustomDialect();
97108
}
98109

99110
public static void resetEndpointCache() {
@@ -110,6 +121,7 @@ public Dialect getDialect(
110121
this.canUpdate = false;
111122
this.dialect = null;
112123

124+
final Dialect customDialect = Driver.getCustomDialect();
113125
if (customDialect != null) {
114126
this.dialectCode = DialectCodes.CUSTOM;
115127
this.dialect = customDialect;

‎wrapper/src/main/java/software/amazon/jdbc/exceptions/ExceptionManager.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,29 @@
1616

1717
package software.amazon.jdbc.exceptions;
1818

19+
import software.amazon.jdbc.Driver;
1920
import software.amazon.jdbc.dialect.Dialect;
2021

2122
public class ExceptionManager {
2223

23-
protected static ExceptionHandler customHandler;
24-
24+
/**
25+
* Sets a custom exception handler.
26+
*
27+
* @deprecated Use software.amazon.jdbc.Driver instead
28+
*/
29+
@Deprecated
2530
public static void setCustomHandler(final ExceptionHandler exceptionHandler) {
26-
customHandler = exceptionHandler;
31+
Driver.setCustomExceptionHandler(exceptionHandler);
2732
}
2833

34+
/**
35+
* Resets a custom exception handler.
36+
*
37+
* @deprecated Use software.amazon.jdbc.Driver instead
38+
*/
39+
@Deprecated
2940
public static void resetCustomHandler() {
30-
customHandler = null;
41+
Driver.resetCustomExceptionHandler();
3142
}
3243

3344
public boolean isLoginException(final Dialect dialect, final Throwable throwable) {
@@ -51,6 +62,7 @@ public boolean isNetworkException(final Dialect dialect, final String sqlState)
5162
}
5263

5364
private ExceptionHandler getHandler(final Dialect dialect) {
65+
final ExceptionHandler customHandler = Driver.getCustomExceptionHandler();
5466
return customHandler != null ? customHandler : dialect.getExceptionHandler();
5567
}
5668
}

0 commit comments

Comments
 (0)