Skip to content

Commit 438f075

Browse files
authored
feat: Add ConnectorRegistry.reset() and update shutdown()
Fixes #1687 Fixes #776
1 parent 533fb04 commit 438f075

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

core/src/main/java/com/google/cloud/sql/ConnectorRegistry.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ public static void shutdown() {
4949
InternalConnectorRegistry.shutdownInstance();
5050
}
5151

52+
/**
53+
* Resets the entire CloudSQL JDBC Connector. This will stop all background threads. The next
54+
* attempt to open a connection or register a configuration will start a new ConnectorRegistry.
55+
*/
56+
public static void reset() {
57+
InternalConnectorRegistry.resetInstance();
58+
}
59+
5260
/**
5361
* Adds an external application name to the user agent string for tracking. This is known to be
5462
* used by the spring-cloud-gcp project.

core/src/main/java/com/google/cloud/sql/core/InternalConnectorRegistry.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public final class InternalConnectorRegistry {
5555
private static final String version = getVersion();
5656
private static final long MIN_REFRESH_DELAY_MS = 30000; // Minimum 30 seconds between refresh.
5757
private static InternalConnectorRegistry internalConnectorRegistry;
58+
private static boolean shutdown = false;
5859
private final ListenableFuture<KeyPair> localKeyPair;
5960
private final ConcurrentHashMap<ConnectorConfig, Connector> unnamedConnectors =
6061
new ConcurrentHashMap<>();
@@ -91,6 +92,10 @@ public final class InternalConnectorRegistry {
9192

9293
/** Returns the {@link InternalConnectorRegistry} singleton. */
9394
public static synchronized InternalConnectorRegistry getInstance() {
95+
if (shutdown) {
96+
throw new IllegalStateException("ConnectorRegistry was shut down.");
97+
}
98+
9499
if (internalConnectorRegistry == null) {
95100
logger.debug("First Cloud SQL connection, generating RSA key pair.");
96101

@@ -114,14 +119,24 @@ public static synchronized InternalConnectorRegistry getInstance() {
114119
* Calls shutdown on the singleton and removes the singleton. After calling shutdownInstance(),
115120
* the next call to getInstance() will start a new singleton instance.
116121
*/
117-
public static synchronized void shutdownInstance() {
122+
public static synchronized void resetInstance() {
118123
if (internalConnectorRegistry != null) {
119124
InternalConnectorRegistry old = internalConnectorRegistry;
120125
internalConnectorRegistry = null;
121126
old.shutdown();
127+
resetUserAgent();
122128
}
123129
}
124130

131+
/**
132+
* Calls shutdown on the singleton and removes the singleton. After calling shutdownInstance(),
133+
* the next call to getInstance() will start a new singleton instance.
134+
*/
135+
public static synchronized void shutdownInstance() {
136+
shutdown = true;
137+
resetInstance();
138+
}
139+
125140
// TODO(kvg): Figure out better executor to use for testing
126141
@VisibleForTesting
127142
// Returns a listenable, scheduled executor that exits upon shutdown.

docs/configuration.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,33 @@ No updates to the database connection pool are required.
182182
Existing open connections in the pool will continue to work until they are
183183
closed. New connections will be established using the new configuration.
184184

185-
### Shutdown The Connector Registry
185+
### Reset The Connector Registry
186186

187187
The application may shut down the ConnectorRegistry. This closes all existing
188188
named and unnamed connectors, and stops internal background threads.
189189

190190
```java
191-
ConnectorRegistry.shutdown();
191+
ConnectorRegistry.reset();
192192
```
193193

194-
After calling `ConnectorRegistry.shutdown()`, the next attempt to connect to a
194+
After calling `ConnectorRegistry.reset()`, the next attempt to connect to a
195195
database using a SocketFactory or R2DBC ConnectionFactory, or
196196
to `ConnectorRegistry.register()` will start a new connector registry, restart
197197
the background threads, and create a new connector.
198198

199+
### Shutdown The Connector Registry
200+
201+
The application may shut down the ConnectorRegistry. This closes all existing
202+
named and unnamed connectors, and stops internal background threads.
203+
204+
```java
205+
ConnectorRegistry.shutdown();
206+
```
207+
208+
After calling `ConnectorRegistry.shutdown()`, subsequent attempts to connect to
209+
a database using a SocketFactory or R2DBC ConnectionFactory, or
210+
to `ConnectorRegistry.register()` will fail, throwing `IllegalStateException`.
211+
199212
## Configuring Google Credentials
200213

201214
By default, connectors will use the Google Application Default credentials to

0 commit comments

Comments
 (0)