Repository: phoenix Updated Branches: refs/heads/4.4-HBase-1.1 404419721 -> 278937023
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/27893702 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/27893702 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/27893702 Branch: refs/heads/4.4-HBase-1.1 Commit: 2789370230305b41c925e7ea164c04ed397d4fe3 Parents: 4044197 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:03:30 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/27893702/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);