@@ -398,26 +398,34 @@ void checkAndCloseSpanners(CheckAndCloseSpannersMode mode) {
398398 keysStillInUse .add (entry .getKey ());
399399 }
400400 }
401- if (keysStillInUse .isEmpty () || mode == CheckAndCloseSpannersMode .WARN ) {
402- if (!keysStillInUse .isEmpty ()) {
401+ try {
402+ if (keysStillInUse .isEmpty () || mode == CheckAndCloseSpannersMode .WARN ) {
403+ if (!keysStillInUse .isEmpty ()) {
404+ logLeakedConnections (keysStillInUse );
405+ logger .log (
406+ Level .WARNING ,
407+ "There is/are "
408+ + keysStillInUse .size ()
409+ + " connection(s) still open."
410+ + " Close all connections before stopping the application" );
411+ }
412+ // Force close all Spanner instances by passing in a value that will always be less than
413+ // the
414+ // difference between the current time and the close time of a connection.
415+ closeUnusedSpanners (Long .MIN_VALUE );
416+ } else {
403417 logLeakedConnections (keysStillInUse );
404- logger . log (
405- Level . WARNING ,
418+ throw SpannerExceptionFactory . newSpannerException (
419+ ErrorCode . FAILED_PRECONDITION ,
406420 "There is/are "
407421 + keysStillInUse .size ()
408- + " connection(s) still open."
409- + " Close all connections before stopping the application" );
422+ + " connection(s) still open. Close all connections before calling closeSpanner()" );
410423 }
411- // Force close all Spanner instances by passing in a value that will always be less than the
412- // difference between the current time and the close time of a connection.
413- closeUnusedSpanners (Long .MIN_VALUE );
414- } else {
415- logLeakedConnections (keysStillInUse );
416- throw SpannerExceptionFactory .newSpannerException (
417- ErrorCode .FAILED_PRECONDITION ,
418- "There is/are "
419- + keysStillInUse .size ()
420- + " connection(s) still open. Close all connections before calling closeSpanner()" );
424+ } finally {
425+ if (closerService != null ) {
426+ closerService .shutdown ();
427+ }
428+ initialized = false ;
421429 }
422430 }
423431 }
0 commit comments