You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by I....@klaverblad.nl on 2006/09/29 13:32:38 UTC

Connection refused exception





We have built a small application based on Apache Httpclient to test the
speed of our database connection. The problem is that we get 'Connection
refused' errors at completely unpredictable moments.

We're creating 50 threads that each wait for a random number of miliseconds
and then do this:

      private boolean execute(String argInsurancePolicyNumber) {

            boolean result = true;

            Connection conn = null;
            PreparedStatement stat = null;
            ResultSet resultSet = null;
            String sql = null;

                  sql = "select * from sysusr.table1 T1_HINT_K0101 where
DEPT_NR = ? and POL_NR = ? and ING_DTM_OCC <= ? and EIND_DTM_OCC > ?";

            try {
                  conn = this.dataSource.getConnection();
                  stat = conn.prepareStatement(sql);

                  stat.setString(1,
argInsurancePolicyNumber.substring(0,2));
                  stat.setString(2,
argInsurancePolicyNumber.substring(2,8));
                  stat.setString(3, "20060505");
                  stat.setString(4, "20060505");

                  resultSet = stat.executeQuery();  // see below for this
method
            }
            catch (Throwable t) {
                  result = false;
                  t.printStackTrace();
            }
            finally {
                  try {
                        resultSet.close();
                  }
                  catch (Throwable t) {
                        t.printStackTrace();
      }
                  try {
                        stat.close();
                  }
                  catch (Throwable t) {
                        t.printStackTrace();
                  }
                  try {
                        conn.close();
                  }
                  catch (Throwable t) {
                        t.printStackTrace();
                  }
            }

            return result;
      }

The 'executeQuery' method eventually does this:

            String tijden = "";

            byte[] responseBody = null;
            int statusCode = 0;

            FireXmlParser parser = null;
            Object[] result = new Object[2];

            this.post = this.createPostMethod("/QW01/W01", 3);  // see
below for this method

            tijden = Long.toString(System.currentTimeMillis()) + ";";

            this.post.addParameter("qedsql", argQuery);

            parser = new FireXmlParser();

            try {
                  tijden = tijden +
Long.toString(System.currentTimeMillis()) + ";";

                  statusCode =
FireXmlHttpURLConnectionApache.client.executeMethod(post);

                  tijden = tijden +
Long.toString(System.currentTimeMillis()) + ";";

                  if (statusCode != HttpStatus.SC_OK) {
                        System.err.println("Method failed: " +
post.getStatusLine());
                  }

                  // Read the response body.
                  responseBody = post.getResponseBody();

                  tijden = tijden +
Long.toString(System.currentTimeMillis()) + ";";

                  parser.parse(responseBody);

                  tijden = tijden +
Long.toString(System.currentTimeMillis()) + ";";

                  result[0] = parser.getResultSet();

                  tijden = tijden +
Long.toString(System.currentTimeMillis()) + ";";

            }
            catch (ConnectException e) {
                  e.printStackTrace();
                  System.err.println("Fatal protocol violation: " +
e.getMessage());
                  result[0] = new SQLException(e.getMessage());
            }
            catch (HttpException e) {
                  System.err.println("Fatal protocol violation: " +
e.getMessage());
            }
            catch (IOException e) {
                  System.err.println("Fatal transport error: " +
e.getMessage());
            }
            finally {
                  post.abort();
                  post.releaseConnection();
    }

            result[1] = tijden;

            this.post.removeParameter("qedsql");
            fireXmlHttpConnectionPoolApache.connectionReady(this);

            return result;

The PostMethod is created like this:

      private PostMethod createPostMethod(String argPath, int
argNumberOfRecoveries) {

            PostMethod post = null;

            post = new PostMethod();
            post.setPath(argPath);
            post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(argNumberOfRecoveries, false));
            post.addRequestHeader("Connection", "close");

            return post;
      }

And the HttpClient is created once, upon initialization, like this:

      private HttpClient createHttpClient(String argHost, int argPort,
                  int argMaxTotalConnections) {

            HttpClient client = null;
            HttpConnectionManagerParams httpConnectionManagerParams = null;

            HostConfiguration hostConfiguration = null;
            MultiThreadedHttpConnectionManager
multiThreadedHttpConnectionManager = null;

            hostConfiguration = new HostConfiguration();
            hostConfiguration.setHost(argHost, argPort);

            httpConnectionManagerParams = new
HttpConnectionManagerParams();
            httpConnectionManagerParams.setStaleCheckingEnabled(false);
            httpConnectionManagerParams

.setDefaultMaxConnectionsPerHost(argMaxTotalConnections);

httpConnectionManagerParams.setMaxConnectionsPerHost(hostConfiguration,
                        argMaxTotalConnections);

httpConnectionManagerParams.setMaxTotalConnections(argMaxTotalConnections);

            multiThreadedHttpConnectionManager = new
MultiThreadedHttpConnectionManager();

multiThreadedHttpConnectionManager.setParams(httpConnectionManagerParams);

            client = new HttpClient(multiThreadedHttpConnectionManager);
            client.setHostConfiguration(hostConfiguration);

            return client;
      }


Please let me know if this doesn't make sense. I find it hard to decide
what to post and what to leave out.

Sometimes it starts throwing exceptions that look like this:

29-sep-2006 9:53:19 org.apache.commons.httpclient.HttpMethodDirector
executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing
request: Connection refused: connect

If this happens we make it go through three more retries (of necessary).
Sometimes that isn't enough and we get:

java.net.ConnectException: Connection refused: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
      at
java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:364)
      at java.net.Socket.connect(Socket.java:507)
      at java.net.Socket.connect(Socket.java:457)
      at java.net.Socket.<init>(Socket.java:365)
      at java.net.Socket.<init>(Socket.java:238)
      at
org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:79)
      at
org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:121)
      at
org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:709)
      at
org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.open(MultiThreadedHttpConnectionManager.java:1321)
      at
org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:393)
      at
org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
      at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
      at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
      etc.

I have been digging through the mail archives and trying to figure this one
out for more than a week but I still don't understand
what *really* causes a 'connection refused'. I guess that is because there
may be many different causes.

Any clues pointing me in the right direction will be much appreciated.

Regards,
Iris


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: Connection refused exception

Posted by Eugeny N Dzhurinsky <bo...@redwerk.com>.
On Fri, Sep 29, 2006 at 02:38:22PM +0200, Roland Weber wrote:
> Hello Iris,
> Connection refused happens on the transport layer below HTTP.
> Check the number of simultaneous connections your HTTP server
> and/or application server can accept or queue.

I'm facing similar problems - I'm getting various java.net.* exceptions
(java.net.SocketTimeoutException, connection reset exception) and "The host did
not accept the connection within timeout of 60000 ms"

At the same time when such exception occurs in application, on the same
computer I can open those links in Firefox and everything works really fast 
without any errors!

-- 
Eugene N Dzhurinsky

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: Connection refused exception

Posted by Roland Weber <RO...@de.ibm.com>.
Hello Iris,

> We have built a small application based on Apache Httpclient to test the
> speed of our database connection. The problem is that we get 'Connection
> refused' errors at completely unpredictable moments.
> 
> We're creating 50 threads that each wait for a random number of 
miliseconds
> and then do this:
> 
> Sometimes it starts throwing exceptions that look like this:
> 
> 29-sep-2006 9:53:19 org.apache.commons.httpclient.HttpMethodDirector
> executeWithRetry
> INFO: I/O exception (java.net.ConnectException) caught when processing
> request: Connection refused: connect
> 
> I have been digging through the mail archives and trying to figure this 
one
> out for more than a week but I still don't understand
> what *really* causes a 'connection refused'. I guess that is because 
there
> may be many different causes.

Connection refused happens on the transport layer below HTTP.
Check the number of simultaneous connections your HTTP server
and/or application server can accept or queue.

hope that helps,
  Roland