@@ -160,7 +160,7 @@ public class RemoteRepositoryManager extends RemoteRepositoryBase implements Aut
160160 private final RemoteTransactionManager transactionManager ;
161161
162162 /**
163- * <code>true</code> iff open .
163+ * <code>true</code> iff already closed .
164164 */
165165 private volatile boolean m_closed = false ;
166166
@@ -367,10 +367,12 @@ public RemoteRepositoryManager(final String serviceURL, final boolean useLBS) {
367367 * trailing "/"). <code>/sparql</code> will be appended to this
368368 * path to obtain the SPARQL end point for the default data set.
369369 * @param httpClient
370- * If the client implements {@link AutoCloseable} then it will be
371- * closed by {@link #close()} (optional). When not present, an
372- * {@link HttpClient} will be allocated and scoped to this
373- * {@link RemoteRepositoryManager} instance.
370+ * The client is managed externally and, in particular,
371+ * may be shared, so we don't close it in {@link #close()}.
372+ * When not present (null), an {@link HttpClient} will be
373+ * allocated and scoped to this
374+ * {@link RemoteRepositoryManager} instance, and closed
375+ * in {@link #close()}.
374376 * @param executor
375377 * An executor used to service http client requests. (optional).
376378 * When not present, an {@link Executor} will be allocated and
@@ -407,10 +409,12 @@ public RemoteRepositoryManager(final String serviceURL, final HttpClient httpCli
407409 * specify <code>true</code>. When <code>false</code>, the REST
408410 * API methods will NOT use the load balancer aware requestURLs.
409411 * @param httpClient
410- * If the client implements {@link AutoCloseable} then it will be
411- * closed by {@link #close()} (optional). When not present, an
412- * {@link HttpClient} will be allocated and scoped to this
413- * {@link RemoteRepositoryManager} instance.
412+ * The client is managed externally and, in particular,
413+ * may be shared, so we don't close it in {@link #close()}.
414+ * When not present (null), an {@link HttpClient} will be
415+ * allocated and scoped to this
416+ * {@link RemoteRepositoryManager} instance, and closed
417+ * in {@link #close()}.
414418 * @param executor
415419 * An executor used to service http client requests. (optional).
416420 * When not present, an {@link Executor} will be allocated and
@@ -509,35 +513,42 @@ protected void finalize() throws Throwable {
509513 @ Override
510514 public void close () throws Exception {
511515
512- if (! m_closed ) {
516+ if (m_closed ) {
513517 // Already closed.
514518 return ;
515519 }
516520
517- if (httpClient instanceof AutoCloseable ) {
518-
519- /*
520- * If the caller passed in an AutoCloseable HttpClient, then we shut
521- * it down now.
522- */
523- ((AutoCloseable ) httpClient ).close ();
524-
525- }
526521
527522 if (our_httpClient != null ) {
528-
523+
529524 /*
530525 * This HttpClient was allocated by our constructor. We will shut it
531526 * down now (unless it is already stopping or stopped).
532527 */
533528
534- if (!our_httpClient .isStopping () && !our_httpClient .isStopped ()) {
529+ if (our_httpClient instanceof AutoCloseable ) {
530+ // The instance is managed by AutoCloseHttpClient or a similar
531+ // class, so close() instead of just stop():
532+ ((AutoCloseable ) our_httpClient ).close ();
533+ } else {
534+ // Not an AutoCloseable, but we should still try to stop it:
535+
536+ if (!our_httpClient .isStopping () && !our_httpClient .isStopped ()) {
535537
536- our_httpClient .stop ();
538+ our_httpClient .stop ();
537539
540+ }
538541 }
539542
540543 }
544+
545+ // Note that we do not close httpClient here, unless it is
546+ // our_httpClient, because it came as
547+ // a parameter to a constructor, may be shared by multiple
548+ // RemoteRepositoryManager instances and other objects, and
549+ // thus has to be closed elswhere (e.g., QueryEngine.shutdown()).
550+
551+
541552
542553 if (our_executor != null ) {
543554
0 commit comments