You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by td...@apache.org on 2015/06/24 22:24:06 UTC

[10/49] phoenix git commit: PHOENIX-1976 Exit gracefully if addShutdownHook fails.

PHOENIX-1976 Exit gracefully if addShutdownHook fails.

If the JVM is already in the process of shutting down,
we don't need to add the shutdown hook for the PhoenixDriver
instance. Additionally, we shouldn't advertise this instance
either since we're going down.


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/23f5acf8
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/23f5acf8
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/23f5acf8

Branch: refs/heads/json
Commit: 23f5acf86e1065f6bc8c342df4ba29f18aafea8a
Parents: 289a875
Author: Josh Elser <jo...@gmail.com>
Authored: Thu May 14 17:40:46 2015 -0400
Committer: Nick Dimiduk <nd...@apache.org>
Committed: Fri May 15 11:05:05 2015 -0700

----------------------------------------------------------------------
 .../org/apache/phoenix/jdbc/PhoenixDriver.java  | 46 ++++++++++++++------
 1 file changed, 32 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/23f5acf8/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDriver.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDriver.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDriver.java
index 6360d06..cfabe82 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDriver.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDriver.java
@@ -60,25 +60,43 @@ public final class PhoenixDriver extends PhoenixEmbeddedDriver {
     private static volatile String driverShutdownMsg;
     static {
         try {
-            DriverManager.registerDriver( INSTANCE = new PhoenixDriver() );
-            // Add shutdown hook to release any resources that were never closed
-            // In theory not necessary, but it won't hurt anything
-            Runtime.getRuntime().addShutdownHook(new Thread() {
-                @Override
-                public void run() {
-                    try {
-                        INSTANCE.close();
-                    } catch (SQLException e) {
-                        logger.warn("Unable to close PhoenixDriver on shutdown", e);
-                    } finally {
-                        driverShutdownMsg = "Phoenix driver closed because server is shutting down";
+            INSTANCE = new PhoenixDriver();
+            try {
+                // Add shutdown hook to release any resources that were never closed
+                // In theory not necessary, but it won't hurt anything
+                Runtime.getRuntime().addShutdownHook(new Thread() {
+                    @Override
+                    public void run() {
+                        closeInstance(INSTANCE);
                     }
-                }
-            });
+                });
+
+                // Only register the driver when we successfully register the shutdown hook
+                // Don't want to register it if we're already in the process of going down.
+                DriverManager.registerDriver( INSTANCE );
+            } catch (IllegalStateException e) {
+                logger.warn("Failed to register PhoenixDriver shutdown hook as the JVM is already shutting down");
+
+                // Close the instance now because we don't have the shutdown hook
+                closeInstance(INSTANCE);
+
+                throw e;
+            }
         } catch (SQLException e) {
             throw new IllegalStateException("Unable to register " + PhoenixDriver.class.getName() + ": "+ e.getMessage());
         }
     }
+
+    private static void closeInstance(PhoenixDriver instance) {
+        try {
+            instance.close();
+        } catch (SQLException e) {
+            logger.warn("Unable to close PhoenixDriver on shutdown", e);
+        } finally {
+            driverShutdownMsg = "Phoenix driver closed because server is shutting down";
+        }
+    }
+
     // One entry per cluster here
     private final ConcurrentMap<ConnectionInfo,ConnectionQueryServices> connectionQueryServicesMap = new ConcurrentHashMap<ConnectionInfo,ConnectionQueryServices>(3);