You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Tim Stibbs <st...@gmail.com> on 2011/05/16 10:06:36 UTC

Tomcat 5.5 doesn't support keep-alive?

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();
}

Re: Tomcat 5.5 doesn't support keep-alive?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tim,

On 5/16/2011 4:06 AM, Tim Stibbs wrote:
> 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.

The Javadoc for java.net.URL has this to say about the openConnection
method:

"A new connection is opened every time by calling the openConnection
method of the protocol handler for this URL."

That seems to be conflicting with this other documentation:

http://download.oracle.com/javase/1.5.0/docs/guide/net/http-keepalive.html

... which says that keep-alive is transparent. I don't see you setting a
"Connection: keep-alive" header, which you probably should be doing. The
example code in the above document does /not/ explicitly set this
header, but it couldn't hurt.

You should also check this out:
http://forums.java.net/node/656951

Evidently, not properly reading the server's response could be a
problem, too. Note that HttpURLConnection requires that you monitor two
separate streams for responses: the output and error streams. You should
drain one or the other stream based upon the response code you get from
the server.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk3RTe0ACgkQ9CaO5/Lv0PBvrQCcDwOPEO/GZtHAHSuUf1FO23M/
Vh4AoKvSDDZWe6hjtAlHnENWG1ixbdAf
=QG3e
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org