Thanks for your suggestions, I have previously been through these options, the code I posted was a shortest path to reproducing my issue. I am ensuring I read the whole response, and any error response.
Setting the keep-alive header appears to make no difference (compared with setting nothing) when connecting to either tomcat or httpd. Setting "Connection: Close" causes it to run out of addresses very quickly when connecting to both tomcat and httpd. The fact that connection close causes it to run out of addresses so much quicker than with keep alive suggests that there is _some_ keep-alive type behaviour going on, i.e. it is managing to re-use a certain number of connections, but not enough. Maybe I'm not really understanding what keep-alive is/does, or I'm overestimating what tomcat is capable of, but I would expect the java URLConnection to behave in a pretty similar way whether connected to tomcat or httpd. As its tomcat whose behaviour is unexpected, I'm hoping someone here can help me. Thanks again, Tim. On 17 May 2011 09:10, <users-h...@tomcat.apache.org> wrote: > > > > ---------- Forwarded message ---------- > From: Tim Stibbs <stripybad...@gmail.com> > To: users@tomcat.apache.org > Date: Mon, 16 May 2011 09:06:36 +0100 > Subject: 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(); > } > > > ---------- Forwarded message ---------- > From: Christopher Schultz <ch...@christopherschultz.net> > To: Tomcat Users List <users@tomcat.apache.org> > Date: Mon, 16 May 2011 12:16:46 -0400 > Subject: Re: Tomcat 5.5 doesn't support keep-alive? > -----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----- > > >