You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2016/10/14 19:22:09 UTC

[10/15] jena git commit: Managing HTTP resources more tightly

Managing HTTP resources more tightly


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/d193bc01
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/d193bc01
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/d193bc01

Branch: refs/heads/master
Commit: d193bc01994e4a34b3ffe28ba04bef77410b3830
Parents: 39b708f
Author: ajs6f <aj...@virginia.edu>
Authored: Sat Oct 8 09:14:54 2016 -0400
Committer: ajs6f <aj...@virginia.edu>
Committed: Thu Oct 13 16:09:42 2016 -0400

----------------------------------------------------------------------
 .../java/org/apache/jena/riot/web/HttpOp.java   | 68 +++++++++++++-------
 .../jena/sparql/engine/http/HttpQuery.java      |  1 -
 .../sparql/engine/http/QueryEngineHTTP.java     | 12 +++-
 .../java/org/apache/jena/fuseki/TS_Fuseki.java  | 12 ++--
 .../AbstractJenaConnectionTests.java            | 24 +++----
 .../org/apache/jena/jdbc/utils/TestUtils.java   | 21 +++---
 .../jena/jdbc/remote/RemoteEndpointDriver.java  |  9 ++-
 .../jena/jdbc/remote/TS_JdbcDriverRemote.java   | 21 ++++++
 .../AbstractRemoteEndpointConnectionTests.java  |  5 +-
 .../AbstractRemoteEndpointResultSetTests.java   |  1 -
 10 files changed, 106 insertions(+), 68 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/d193bc01/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java b/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java
index dd06c58..48b1b0b 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java
@@ -34,11 +34,10 @@ import org.apache.http.client.methods.* ;
 import org.apache.http.entity.ContentType ;
 import org.apache.http.entity.InputStreamEntity ;
 import org.apache.http.entity.StringEntity ;
+import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder ;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.impl.client.cache.CachingHttpClientBuilder;
-import org.apache.http.impl.conn.PoolingClientConnectionManager;
-import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
 import org.apache.http.message.BasicNameValuePair ;
 import org.apache.http.protocol.HttpContext ;
 import org.apache.http.util.EntityUtils ;
@@ -140,13 +139,38 @@ public class HttpOp {
      */
     public static class CaptureInput implements HttpCaptureResponse<TypedInputStream> {
         private TypedInputStream stream;
+        
+        private boolean closeClient = false;
+        
+        private CloseableHttpClient client;
+        
+        public void setClient(CloseableHttpClient client) {
+            this.client = client;
+        }
+
+        private static class ClientRetainingTypedInputStream extends TypedInputStream {
+
+            private final CloseableHttpClient retainedClient;
+            
+            public ClientRetainingTypedInputStream(InputStream in, String contentType, CloseableHttpClient client) {
+                super(in, contentType);
+                this.retainedClient = client;
+            }
+
+            @Override
+            public void close() {
+                IO.close(retainedClient);
+                super.close();
+            }
+            
+        }
 
         @Override
         public void handle(String baseIRI, HttpResponse response) throws IOException {
-
             HttpEntity entity = response.getEntity();
             String ct = (entity.getContentType() == null) ? null : entity.getContentType().getValue();
-            stream = new TypedInputStream(entity.getContent(), ct);
+            stream = closeClient ? new ClientRetainingTypedInputStream(entity.getContent(), ct, client)
+                    : new TypedInputStream(entity.getContent(), ct);
         }
 
         @Override
@@ -200,7 +224,7 @@ public class HttpOp {
             .build() ;
     }
     
-    public static HttpClient createCachingHttpClient() {
+    public static CloseableHttpClient createCachingHttpClient() {
         String s = System.getProperty("http.maxConnections", "5");
         int max = Integer.parseInt(s);
         return CachingHttpClientBuilder.create()
@@ -1026,7 +1050,18 @@ public class HttpOp {
 
     // ---- Perform the operation!
     private static void exec(String url, HttpUriRequest request, String acceptHeader, HttpResponseHandler handler, HttpClient httpClient, HttpContext httpContext) {
-        httpClient = ensureClient(httpClient);
+        // whether we should close the client after request execution
+        // only true if we built the client right here
+        boolean closeClient = false;
+        if (httpClient == null) {
+            if (getDefaultHttpClient() == null ) {
+                httpClient = HttpClients.createMinimal();
+                closeClient = true;
+            }
+            else httpClient = getDefaultHttpClient();
+        }
+        // and also only true if the handler won't close the client for us
+        closeClient = closeClient && !(handler instanceof CaptureInput);
         try {
             if (handler == null)
                 // This cleans up.
@@ -1057,6 +1092,8 @@ public class HttpOp {
             // Redirects are followed by HttpClient.
             if (handler != null)
                 handler.handle(baseURI, response);
+            // the cast below is safe because if closeClient then we built the client in this method 
+            if (closeClient) IO.close((CloseableHttpClient) httpClient);
         } catch (IOException ex) {
             throw new HttpException(ex);
         }
@@ -1067,25 +1104,6 @@ public class HttpOp {
 	}
 
     /**
-     * Ensures that a HTTP Client is non-null
-     * <p>
-     * Prefers the {@link HttpClient} provided for the request if available.
-     * Then it tries to use a Jena-wide user configurable {@link HttpClient} if available.
-     * </p>
-     * <p>
-     * In all other cases it creates a fresh instance of a client each time.
-     * </p>
-     * 
-     * @param client HTTP Client
-     * @return HTTP Client
-     */
-    public static HttpClient ensureClient(HttpClient client) {
-        return client != null ? client
-                : getDefaultHttpClient() != null ? getDefaultHttpClient()
-                        : HttpClients.createSystem();
-    }
-
-    /**
      * Applies the configured User-Agent string to the HTTP request
      * 
      * @param message

http://git-wip-us.apache.org/repos/asf/jena/blob/d193bc01/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/HttpQuery.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/HttpQuery.java b/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/HttpQuery.java
index c9673d9..c13eb69 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/HttpQuery.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/HttpQuery.java
@@ -283,7 +283,6 @@ public class HttpQuery extends Params {
      */
     public InputStream exec() throws QueryExceptionHTTP {
         // Select the appropriate HttpClient to use
-        client = HttpOp.ensureClient(getClient());
         contextualizeCompressionSettings();
         contextualizeTimeoutSettings();
         try {

http://git-wip-us.apache.org/repos/asf/jena/blob/d193bc01/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/QueryEngineHTTP.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/QueryEngineHTTP.java b/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/QueryEngineHTTP.java
index 7833ab3..d250878 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/QueryEngineHTTP.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/engine/http/QueryEngineHTTP.java
@@ -135,8 +135,7 @@ public class QueryEngineHTTP implements QueryExecution {
         this.query = query;
         this.queryString = queryString;
         this.service = serviceURI;
-        // Copy the global context to freeze it.
-        this.context = new Context(ARQ.getContext());
+        this.context = ARQ.getContext();
 
         // Apply service configuration if relevant
         applyServiceConfig(serviceURI, this);
@@ -613,6 +612,15 @@ public class QueryEngineHTTP implements QueryExecution {
         if (params != null) httpQuery.merge(params);
 
         httpQuery.setAllowCompression(allowCompression);
+        
+        // check for service context overrides
+        if (context.isDefined(Service.serviceContext)) {
+            Map<String, Context> servicesContext = context.get(Service.serviceContext);
+            if (servicesContext.containsKey(service)) {
+                Context serviceContext = servicesContext.get(service);
+                if (serviceContext.isDefined(Service.queryClient)) client = serviceContext.get(Service.queryClient);
+            }
+        }
         httpQuery.setClient(client);
         httpQuery.setContext(getHttpContext());
         

http://git-wip-us.apache.org/repos/asf/jena/blob/d193bc01/jena-fuseki1/src/test/java/org/apache/jena/fuseki/TS_Fuseki.java
----------------------------------------------------------------------
diff --git a/jena-fuseki1/src/test/java/org/apache/jena/fuseki/TS_Fuseki.java b/jena-fuseki1/src/test/java/org/apache/jena/fuseki/TS_Fuseki.java
index 693217f..30e8ee3 100644
--- a/jena-fuseki1/src/test/java/org/apache/jena/fuseki/TS_Fuseki.java
+++ b/jena-fuseki1/src/test/java/org/apache/jena/fuseki/TS_Fuseki.java
@@ -18,7 +18,7 @@
 
 package org.apache.jena.fuseki;
 
-import org.apache.http.client.HttpClient ;
+import org.apache.http.client.HttpClient;
 import org.apache.jena.atlas.logging.LogCtl ;
 import org.apache.jena.fuseki.http.TestDatasetAccessorHTTP ;
 import org.apache.jena.fuseki.http.TestDatasetGraphAccessorHTTP ;
@@ -40,16 +40,14 @@ import org.junit.runners.Suite ;
 })
 public class TS_Fuseki extends ServerTest
 {
-    // Use HttpOp caching of connections during testing to stop
-    // swamping  kernel socket management (seems to be most
-    // acute on Java 1.6)
-    
+    // Use HttpOp caching of connections during testing to avoid
+    // swamping  kernel socket management
     static HttpClient defaultHttpClient = HttpOp.getDefaultHttpClient() ;
     // Used for all tests except auth tests.
-    static HttpClient globalCachingClient = HttpOp.createCachingHttpClient() ;
+    static final HttpClient globalPoolingClient = HttpOp.createPoolingHttpClient();
     
     @BeforeClass public static void beforeClassAbstract1() {
-        HttpOp.setDefaultHttpClient(globalCachingClient) ;
+        HttpOp.setDefaultHttpClient(globalPoolingClient) ;
     }
     
     @AfterClass public static void afterClassAbstract1() {

http://git-wip-us.apache.org/repos/asf/jena/blob/d193bc01/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java
----------------------------------------------------------------------
diff --git a/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java b/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java
index e59d01d..76696ce 100644
--- a/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java
+++ b/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/connections/AbstractJenaConnectionTests.java
@@ -130,7 +130,7 @@ public abstract class AbstractJenaConnectionTests {
      */
     @Test
     public void connection_create_close_02() throws SQLException {
-        JenaConnection conn = this.getConnection(DatasetFactory.createMem());
+        JenaConnection conn = this.getConnection(DatasetFactory.createTxnMem());
         Assert.assertFalse(conn.isClosed());
         conn.close();
         Assert.assertTrue(conn.isClosed());
@@ -228,7 +228,7 @@ public abstract class AbstractJenaConnectionTests {
     @Test
     public void connection_statement_query_select_02() throws SQLException {
         // Prepare a dataset
-        Dataset ds = DatasetFactory.createMem();
+        Dataset ds = DatasetFactory.createTxnMem();
         ds.asDatasetGraph().add(
                 new Quad(NodeFactory.createURI("http://example/graph"), NodeFactory.createURI("http://example/subject"),
                         NodeFactory.createURI("http://example/predicate"), NodeFactory.createURI("http://example/object")));
@@ -272,7 +272,7 @@ public abstract class AbstractJenaConnectionTests {
     @Test
     public void connection_statement_query_select_03() throws SQLException {
         // Prepare a dataset
-        Dataset ds = DatasetFactory.createMem();
+        Dataset ds = DatasetFactory.createTxnMem();
         ds.asDatasetGraph().add(
                 new Quad(NodeFactory.createURI("http://example/graph"), NodeFactory.createURI("http://example/subject"),
                         NodeFactory.createURI("http://example/predicate"), NodeFactory.createURI("http://example/object")));
@@ -317,7 +317,7 @@ public abstract class AbstractJenaConnectionTests {
     @Test
     public void connection_statement_query_select_04() throws SQLException {
         // Prepare a dataset
-        Dataset ds = DatasetFactory.createMem();
+        Dataset ds = DatasetFactory.createTxnMem();
         ds.asDatasetGraph().add(
                 new Quad(NodeFactory.createURI("http://example/graph"), NodeFactory.createURI("http://example/subject"),
                         NodeFactory.createURI("http://example/predicate"), NodeFactory.createURI("http://example/object")));
@@ -362,7 +362,7 @@ public abstract class AbstractJenaConnectionTests {
     @Test
     public void connection_prepared_statement_select_01() throws SQLException, MalformedURLException {
         // Prepare a dataset
-        Dataset ds = DatasetFactory.createMem();
+        Dataset ds = DatasetFactory.createTxnMem();
         ds.asDatasetGraph().add(
                 new Quad(NodeFactory.createURI("http://example/graph"), NodeFactory.createURI("http://example/subject"),
                         NodeFactory.createURI("http://example/predicate"), NodeFactory.createURI("http://example/object")));
@@ -410,7 +410,7 @@ public abstract class AbstractJenaConnectionTests {
     @Test
     public void connection_prepared_statement_select_02() throws SQLException {
         // Prepare a dataset
-        Dataset ds = DatasetFactory.createMem();
+        Dataset ds = DatasetFactory.createTxnMem();
         ds.asDatasetGraph().add(
                 new Quad(NodeFactory.createURI("http://example/graph"), NodeFactory.createURI("http://example/subject"),
                         NodeFactory.createURI("http://example/predicate"), NodeFactory.createLiteral("value")));
@@ -458,7 +458,7 @@ public abstract class AbstractJenaConnectionTests {
     @Test
     public void connection_prepared_statement_select_03() throws SQLException {
         // Prepare a dataset
-        Dataset ds = DatasetFactory.createMem();
+        Dataset ds = DatasetFactory.createTxnMem();
         ds.asDatasetGraph().add(
                 new Quad(NodeFactory.createURI("http://example/graph"), NodeFactory.createURI("http://example/subject"),
                         NodeFactory.createURI("http://example/predicate"), NodeFactory.createLiteral("value")));
@@ -886,7 +886,7 @@ public abstract class AbstractJenaConnectionTests {
     @Test
     public void connection_statement_query_construct_02() throws SQLException {
         // Prepare a dataset
-        Dataset ds = DatasetFactory.createMem();
+        Dataset ds = DatasetFactory.createTxnMem();
         ds.asDatasetGraph().add(
                 new Quad(NodeFactory.createURI("http://example/graph"), NodeFactory.createURI("http://example/subject"),
                         NodeFactory.createURI("http://example/predicate"), NodeFactory.createURI("http://example/object")));
@@ -959,7 +959,7 @@ public abstract class AbstractJenaConnectionTests {
     @Test
     public void connection_statement_query_describe_02() throws SQLException {
         // Prepare a dataset
-        Dataset ds = DatasetFactory.createMem();
+        Dataset ds = DatasetFactory.createTxnMem();
         ds.asDatasetGraph().add(
                 new Quad(NodeFactory.createURI("http://example/graph"), NodeFactory.createURI("http://example/subject"),
                         NodeFactory.createURI("http://example/predicate"), NodeFactory.createURI("http://example/object")));
@@ -1010,7 +1010,7 @@ public abstract class AbstractJenaConnectionTests {
     @Test
     public void connection_transactions_01() throws SQLException {
         // Set up connection
-        JenaConnection conn = this.getConnection(DatasetFactory.createMem());
+        JenaConnection conn = this.getConnection(DatasetFactory.createTxnMem());
         Assume.assumeNotNull(conn.getMetaData());
         Assume.assumeTrue(conn.getMetaData().supportsTransactions());
         conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
@@ -1044,7 +1044,7 @@ public abstract class AbstractJenaConnectionTests {
     @Test
     public void connection_transactions_02() throws SQLException {
         // Set up connection
-        JenaConnection conn = this.getConnection(DatasetFactory.createMem());
+        JenaConnection conn = this.getConnection(DatasetFactory.createTxnMem());
         Assume.assumeNotNull(conn.getMetaData());
         Assume.assumeTrue(conn.getMetaData().supportsTransactions());
         conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
@@ -1082,7 +1082,7 @@ public abstract class AbstractJenaConnectionTests {
     @Test
     public void connection_transactions_03() throws SQLException {
         // Set up connection
-        JenaConnection conn = this.getConnection(DatasetFactory.createMem());
+        JenaConnection conn = this.getConnection(DatasetFactory.createTxnMem());
         Assume.assumeNotNull(conn.getMetaData());
         Assume.assumeTrue(conn.getMetaData().supportsTransactions());
         conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);

http://git-wip-us.apache.org/repos/asf/jena/blob/d193bc01/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/utils/TestUtils.java
----------------------------------------------------------------------
diff --git a/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/utils/TestUtils.java b/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/utils/TestUtils.java
index 42a1c08..85ba997 100644
--- a/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/utils/TestUtils.java
+++ b/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/utils/TestUtils.java
@@ -18,10 +18,12 @@
 
 package org.apache.jena.jdbc.utils;
 
+import static org.apache.jena.graph.Node.ANY;
+import static org.apache.jena.sparql.core.Quad.defaultGraphIRI;
+
 import java.util.Iterator;
 
 import org.apache.http.client.HttpClient;
-import org.apache.jena.graph.Node ;
 import org.apache.jena.query.Dataset ;
 import org.apache.jena.query.DatasetAccessor ;
 import org.apache.jena.query.DatasetAccessorFactory ;
@@ -29,7 +31,6 @@ import org.apache.jena.query.DatasetFactory ;
 import org.apache.jena.rdf.model.Model ;
 import org.apache.jena.rdf.model.ModelFactory ;
 import org.apache.jena.sparql.core.DatasetGraph ;
-import org.apache.jena.sparql.core.Quad ;
 
 /**
  * Test utility methods
@@ -53,7 +54,7 @@ public class TestUtils {
         if (triplesPerGraph <= 0)
             throw new IllegalArgumentException("Number of triples per graph must be >= 1");
 
-        Dataset ds = DatasetFactory.createMem();
+        Dataset ds = DatasetFactory.createTxnMem();
         if (createDefaultGraph) {
             numGraphs--;
             Model def = ModelFactory.createDefaultModel();
@@ -103,21 +104,15 @@ public class TestUtils {
     public static void copyDataset(Dataset source, Dataset target, boolean copyDefaultAsQuads) {
         // Copy the default graph
         if (copyDefaultAsQuads) {
-            Iterator<Quad> quads = source.asDatasetGraph().find(Quad.defaultGraphIRI, Node.ANY, Node.ANY, Node.ANY);
             DatasetGraph targetDSG = target.asDatasetGraph();
-            while (quads.hasNext()) {
-                targetDSG.add(quads.next());
-            }
+            source.asDatasetGraph().find(defaultGraphIRI, ANY, ANY, ANY).forEachRemaining(targetDSG::add);
         } else {
             target.setDefaultModel(source.getDefaultModel());
         }
 
         // Copy named graphs
-        Iterator<String> uris = source.listNames();
-        while (uris.hasNext()) {
-            String uri = uris.next();
-            target.addNamedModel(uri, source.getNamedModel(uri));
-        }
+        source.listNames().forEachRemaining(uri->target.addNamedModel(uri, source.getNamedModel(uri)));
+
     }
 
     /**
@@ -168,7 +163,7 @@ public class TestUtils {
      * @return New Dataset
      */
     public static Dataset renameGraph(Dataset ds, String oldUri, String newUri) {
-        Dataset dest = DatasetFactory.createMem();
+        Dataset dest = DatasetFactory.createTxnMem();
         if (oldUri == null) {
             // Rename default graph
             dest.addNamedModel(newUri, ds.getDefaultModel());

http://git-wip-us.apache.org/repos/asf/jena/blob/d193bc01/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java
----------------------------------------------------------------------
diff --git a/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java b/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java
index 269cd90..2526cae 100644
--- a/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java
+++ b/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java
@@ -260,9 +260,12 @@ public class RemoteEndpointDriver extends JenaDriver {
         }
         // else use a supplied or default client
         Object client = props.get(PARAM_CLIENT);
-        if (client != null && !(client instanceof HttpClient)) { throw new SQLException("The " + PARAM_CLIENT
-                + " parameter is specified but the value is not an object implementing the required HttpClient interface"); }
-        return HttpOp.ensureClient((HttpClient) client);
+        if (client != null) {
+            if (!(client instanceof HttpClient)) throw new SQLException("The " + PARAM_CLIENT
+                    + " parameter is specified but the value is not an object implementing the required HttpClient interface");
+            return (HttpClient) client;
+        }
+        return null;
     }
 
 

http://git-wip-us.apache.org/repos/asf/jena/blob/d193bc01/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/TS_JdbcDriverRemote.java
----------------------------------------------------------------------
diff --git a/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/TS_JdbcDriverRemote.java b/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/TS_JdbcDriverRemote.java
index b29bc9d..1b6eb36 100644
--- a/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/TS_JdbcDriverRemote.java
+++ b/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/TS_JdbcDriverRemote.java
@@ -17,6 +17,7 @@
  */
 package org.apache.jena.jdbc.remote;
 
+import org.apache.http.client.HttpClient;
 import org.apache.jena.jdbc.remote.connections.TestRemoteEndpointConnection;
 import org.apache.jena.jdbc.remote.connections.TestRemoteEndpointConnectionWithAuth;
 import org.apache.jena.jdbc.remote.connections.TestRemoteEndpointConnectionWithGraphUris;
@@ -27,6 +28,9 @@ import org.apache.jena.jdbc.remote.results.TestRemoteEndpointResultsWithAuth;
 import org.apache.jena.jdbc.remote.results.TestRemoteEndpointResultsWithGraphUris;
 import org.apache.jena.jdbc.remote.results.TestRemoteEndpointResultsWithResultSetTypes;
 import org.apache.jena.jdbc.remote.statements.TestRemoteEndpointStatements;
+import org.apache.jena.riot.web.HttpOp;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
 import org.junit.runner.RunWith;
 import org.junit.runners.Suite;
 
@@ -51,4 +55,21 @@ import org.junit.runners.Suite;
 
 
 public class TS_JdbcDriverRemote {
+
+    // Use HttpOp caching of connections during testing to avoid
+    // swamping kernel socket management
+    static HttpClient defaultHttpClient = HttpOp.getDefaultHttpClient();
+    // Used for all tests except auth tests.
+    static final HttpClient globalPoolingClient = HttpOp.createPoolingHttpClient();
+
+    @BeforeClass
+    public static void beforeClassAbstract1() {
+        HttpOp.setDefaultHttpClient(globalPoolingClient);
+    }
+
+    @AfterClass
+    public static void afterClassAbstract1() {
+        HttpOp.setDefaultHttpClient(defaultHttpClient);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/d193bc01/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/connections/AbstractRemoteEndpointConnectionTests.java
----------------------------------------------------------------------
diff --git a/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/connections/AbstractRemoteEndpointConnectionTests.java b/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/connections/AbstractRemoteEndpointConnectionTests.java
index 8d7e83c..08da404 100644
--- a/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/connections/AbstractRemoteEndpointConnectionTests.java
+++ b/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/connections/AbstractRemoteEndpointConnectionTests.java
@@ -15,7 +15,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.jena.jdbc.remote.connections;
 
 import org.apache.jena.fuseki.Fuseki;
@@ -23,12 +22,10 @@ import org.apache.jena.jdbc.connections.AbstractJenaConnectionTests;
 
 /**
  * Abstract tests for remote endpoint driver connections
- *
  */
 public abstract class AbstractRemoteEndpointConnectionTests extends AbstractJenaConnectionTests {
-    
+
     static {
         Fuseki.init();
     }
-
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/d193bc01/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/results/AbstractRemoteEndpointResultSetTests.java
----------------------------------------------------------------------
diff --git a/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/results/AbstractRemoteEndpointResultSetTests.java b/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/results/AbstractRemoteEndpointResultSetTests.java
index 53d507b..f1b965d 100644
--- a/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/results/AbstractRemoteEndpointResultSetTests.java
+++ b/jena-jdbc/jena-jdbc-driver-remote/src/test/java/org/apache/jena/jdbc/remote/results/AbstractRemoteEndpointResultSetTests.java
@@ -21,7 +21,6 @@ package org.apache.jena.jdbc.remote.results;
 import org.apache.http.client.HttpClient;
 import org.apache.jena.fuseki.Fuseki;
 import org.apache.jena.jdbc.results.AbstractResultSetTests;
-import org.apache.jena.query.ARQ ;
 import org.apache.jena.riot.web.HttpOp;
 import org.apache.jena.system.JenaSystem ;
 import org.junit.AfterClass;