• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

URLConnection problem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I'm having a problem using URLConnections that I'm hoping someone can shed some light on. The basic problem is: if a client creates numerous URLConnections very rapidly, eventually it stops being able to open any more connections without waiting a few minutes for a resource (client ports?) to be released.

I've simplified the problem down to a relatively simple test case:

I run Tomcat, and drop a simple HTML file in the root. I then run a small Java program that repeatedly opens a URLConnection to the file, reads it, and releases the connection. I find that on the 3963rd attempt, the connection fails. After that, any more attempts fail for approximately 2 minutes. Then it works again and fails on the 3963rd attempt again. If I run "netstat" in a command prompt right after running the test program, a huge list of connections is displayed. This list seems to clear about the same time the program starts to work again.

I wrote an identical program that uses sockets to connect instead of URLConnections, and it can run indefinitely without ever getting errors. However if I run it right after the URLConnection test has failed, it fails as well.

What I've concluded from these tests is that the URLConnection class does not release some resource (I believe this resource is "client ports" allocated by Windows) for about two minutes. Even calling HttpURLConnection.disconnect() does not release this resource. Sockets seem to use the same resource, but release this resource immediately when close() is called.

Does anyone know of a way to get the URLConnection class to release this resource immediately? Or any workaround other than writing all my HTTP communication from scratch using sockets?

Information about the platform I'm using:
- Windows XP
- Java 1.3.1_08 (also tested with 1.4.2_02 - same behavior)


Here is my test program that uses URLConnections:



And here is my equivalent test program that uses sockets:



Thanks in advance for any help..
Will Ross
www.inmedius.com
[ May 27, 2004: Message edited by: Will Ross ]
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested your code with:

OS: Linux 2.6.5
Webserver: xitami v2.5b5
java: 1.5b and java: 1.4.2

runs fine with 7000 requests for both javas,
and - only tested with 1.5: for 70.000 requests - no pause involved, and memory monitoring showed constant value.
 
Will Ross
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes; I'm pretty sure that this is only a problem with Windows. Although I assume there is some limit to available ports/file descriptors on other OS's also, it might be very high - 64k or something.

I saw a similar discussion here indicating that this is just a Windows issue.


Originally posted by Stefan Wagner:
I tested your code with:

OS: Linux 2.6.5
Webserver: xitami v2.5b5
java: 1.5b and java: 1.4.2

runs fine with 7000 requests for both javas,
and - only tested with 1.5: for 70.000 requests - no pause involved, and memory monitoring showed constant value.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic