From 208674632b20b37f51b828c1c4cc76c91154952b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Fri, 9 Oct 2020 04:49:03 +0200 Subject: [PATCH] fix: close executor when closing pool (#501) --- .../cloud/spanner/connection/SpannerPool.java | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/SpannerPool.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/SpannerPool.java index 350cf61394..de351c87c9 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/SpannerPool.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/SpannerPool.java @@ -398,26 +398,34 @@ void checkAndCloseSpanners(CheckAndCloseSpannersMode mode) { keysStillInUse.add(entry.getKey()); } } - if (keysStillInUse.isEmpty() || mode == CheckAndCloseSpannersMode.WARN) { - if (!keysStillInUse.isEmpty()) { + try { + if (keysStillInUse.isEmpty() || mode == CheckAndCloseSpannersMode.WARN) { + if (!keysStillInUse.isEmpty()) { + logLeakedConnections(keysStillInUse); + logger.log( + Level.WARNING, + "There is/are " + + keysStillInUse.size() + + " connection(s) still open." + + " Close all connections before stopping the application"); + } + // Force close all Spanner instances by passing in a value that will always be less than + // the + // difference between the current time and the close time of a connection. + closeUnusedSpanners(Long.MIN_VALUE); + } else { logLeakedConnections(keysStillInUse); - logger.log( - Level.WARNING, + throw SpannerExceptionFactory.newSpannerException( + ErrorCode.FAILED_PRECONDITION, "There is/are " + keysStillInUse.size() - + " connection(s) still open." - + " Close all connections before stopping the application"); + + " connection(s) still open. Close all connections before calling closeSpanner()"); } - // Force close all Spanner instances by passing in a value that will always be less than the - // difference between the current time and the close time of a connection. - closeUnusedSpanners(Long.MIN_VALUE); - } else { - logLeakedConnections(keysStillInUse); - throw SpannerExceptionFactory.newSpannerException( - ErrorCode.FAILED_PRECONDITION, - "There is/are " - + keysStillInUse.size() - + " connection(s) still open. Close all connections before calling closeSpanner()"); + } finally { + if (closerService != null) { + closerService.shutdown(); + } + initialized = false; } } }