20
20
import java .sql .SQLException ;
21
21
import java .util .List ;
22
22
import java .util .Properties ;
23
- import java .util .concurrent .atomic .AtomicReference ;
24
23
import org .checkerframework .checker .nullness .qual .NonNull ;
25
24
import org .checkerframework .checker .nullness .qual .Nullable ;
26
25
import software .amazon .jdbc .cleanup .CanReleaseResources ;
27
26
28
27
public class ConnectionProviderManager {
29
28
30
- private static AtomicReference <ConnectionProvider > customConnectionProvider = new AtomicReference <>(null );
31
-
32
29
private final ConnectionProvider defaultProvider ;
33
30
private final @ Nullable ConnectionProvider effectiveConnProvider ;
34
31
35
- private static ConnectionInitFunc connectionInitFunc = null ;
36
-
37
32
/**
38
33
* {@link ConnectionProviderManager} constructor.
39
34
*
@@ -57,9 +52,11 @@ public ConnectionProviderManager(
57
52
* {@link ConnectionProvider#acceptsUrl} for more info.
58
53
*
59
54
* @param connProvider the {@link ConnectionProvider} to use to establish new connections
55
+ * @deprecated Use software.amazon.jdbc.Driver instead
60
56
*/
57
+ @ Deprecated
61
58
public static void setConnectionProvider (ConnectionProvider connProvider ) {
62
- customConnectionProvider . set (connProvider );
59
+ Driver . setCustomConnectionProvider (connProvider );
63
60
}
64
61
65
62
/**
@@ -78,9 +75,9 @@ public static void setConnectionProvider(ConnectionProvider connProvider) {
78
75
public ConnectionProvider getConnectionProvider (
79
76
String driverProtocol , HostSpec host , Properties props ) {
80
77
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 ;
84
81
}
85
82
86
83
if (this .effectiveConnProvider != null && this .effectiveConnProvider .acceptsUrl (driverProtocol , host , props )) {
@@ -110,8 +107,8 @@ public ConnectionProvider getDefaultProvider() {
110
107
* return false
111
108
*/
112
109
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 )) {
115
112
return true ;
116
113
}
117
114
@@ -143,10 +140,10 @@ public boolean acceptsStrategy(HostRole role, String strategy) {
143
140
public HostSpec getHostSpecByStrategy (List <HostSpec > hosts , HostRole role , String strategy , Properties props )
144
141
throws SQLException , UnsupportedOperationException {
145
142
HostSpec host = null ;
146
- final ConnectionProvider tmpCustomConnectionProvider = customConnectionProvider . get ();
143
+ final ConnectionProvider customConnectionProvider = Driver . getCustomConnectionProvider ();
147
144
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 );
150
147
}
151
148
} catch (UnsupportedOperationException e ) {
152
149
// 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
170
167
* Clears the non-default {@link ConnectionProvider} if it has been set. The default
171
168
* ConnectionProvider will be used if the non-default ConnectionProvider has not been set or has
172
169
* been cleared.
170
+ *
171
+ * @deprecated Use software.amazon.jdbc.Driver instead
173
172
*/
173
+ @ Deprecated
174
174
public static void resetProvider () {
175
- customConnectionProvider . set ( null );
175
+ Driver . resetCustomConnectionProvider ( );
176
176
}
177
177
178
178
/**
179
179
* Releases any resources held by the available {@link ConnectionProvider} instances.
180
180
*/
181
181
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 ();
185
185
}
186
186
}
187
187
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
188
195
public static void setConnectionInitFunc (final @ NonNull ConnectionInitFunc func ) {
189
- connectionInitFunc = func ;
196
+ Driver . setConnectionInitFunc ( func ) ;
190
197
}
191
198
199
+ /**
200
+ * Resets a custom connection initialization function.
201
+ *
202
+ * @deprecated Use software.amazon.jdbc.Driver instead
203
+ */
204
+ @ Deprecated
192
205
public static void resetConnectionInitFunc () {
193
- connectionInitFunc = null ;
206
+ Driver . resetConnectionInitFunc () ;
194
207
}
195
208
196
209
public void initConnection (
@@ -199,12 +212,12 @@ public void initConnection(
199
212
final @ NonNull HostSpec hostSpec ,
200
213
final @ NonNull Properties props ) throws SQLException {
201
214
202
- final ConnectionInitFunc copy = connectionInitFunc ;
203
- if (copy == null ) {
215
+ final ConnectionInitFunc connectionInitFunc = Driver . getConnectionInitFunc () ;
216
+ if (connectionInitFunc == null ) {
204
217
return ;
205
218
}
206
219
207
- copy .initConnection (connection , protocol , hostSpec , props );
220
+ connectionInitFunc .initConnection (connection , protocol , hostSpec , props );
208
221
}
209
222
210
223
public interface ConnectionInitFunc {
0 commit comments