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/f2be9138
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/f2be9138
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/f2be9138

Branch: refs/heads/4.x-HBase-1.1
Commit: f2be9138359b078fd3e285f3fd441de711789962
Parents: dc46b14
Author: Josh Elser <josh.el...@gmail.com>
Authored: Thu May 14 17:40:46 2015 -0400
Committer: Nick Dimiduk <ndimi...@apache.org>
Committed: Mon Jun 1 12:02:28 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/f2be9138/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);
 

Reply via email to