I have a java client that is making a large number of calls to a servlet
running in a web app in tomcat 5.5.28. I have noticed that if the client
process runs for too long, the client machine runs out of ports throwing a
"java.net.BindException: Address already in use: connect". I thought that
java's URLConnection maintained an internal pool of connections, and that so
long as keep alive was enabled it would re-use the connections.

However, when the server is in tomcat, this does not seem to happen, and it
never sends the keep alive header with the response. I tested it just making
a get request to a single web page (using the code below), in case my
servlet was doing something wrong, but I have the same issue. Note that
putting the same web page in apache httpd 2.2 does not have the same problem
- httpd sends the keep alive header in the response and java manages to pool
the connections properly.

The documentation seems to suggest that tomcat does support keep alive, I
have tried with tomcat 6 which seems to have more keep alive related
settings, but it seems to make no difference.

Note that looking at netstat etc shows that all the old connections are in
time_wait, but because I am making a lot of new connections very often the
old ones haven't timed out and it runs out of local ports.

Can anyone suggest what I might be doing wrong? Any suggestions greatly
appreciated.


while (true)
{
  URLConnection servletConnection = url.openConnection();
  BufferedReader is = new BufferedReader(new
InputStreamReader(servletConnection.getInputStream()));
  String line;
  while ((line = is.readLine()) != null)
  {
  }
  is.close();
}

Reply via email to