k said:
i have created a rudimentary socket connection pool.
each socket connects to an external server then is stored in a vector
for use later. there is no timeout on the connection, the external
server is happy to have connections sitting off it.
(1) It is extremely important to understand that in Java you can never
do anything directly with or to an object. You can access or manipulate
an object only via a reference to that object. The particular
application to your problem is that you don't store Sockets in your
Vector, you store _references_ to Sockets.
Moreover, a Vector doesn't know or care what kind of object any
particular reference it contains refers to. When you think about it in
that light, I hope you see that storing a reference to a Socket in a
Vector is extremely unlikely to be causing your problem -- the Vector
cannot do anything to your Socket that it cannot do to any other Object.
In fact, Vectors don't do anything at all to any of the objects to
which they store references.
(2) The timeout on a socket is the time before any read request times
out, _not_ a timeout on how long the socket will be held open. You
might (or might not) find the keepalive option useful for your intended
purpose, however.
(3) Remember that your socket is only one of two endpoints of a
connection. The other endpoint may be closed completely independently
of anything you do. For a connection pool you must be prepared to deal
with that.
as i create each connection i test it with theSocket.isConnected() to
determine if its good before i add it to the vector.
i can also pull the newly added socket off the vector and test it and
its still good.
Evidently, then, it is not the interaction with a Vector that causes
your problem. But we already knew that.
but later, when i come to retrieve sockets from the pool (fifo via
remove(0)) and test them again as above, they are no longer alive.
My best guess would be that the remote end closed the connections after
they were idle for a certain length of time. As I suggested above,
enabling keepalive on the socket might help, but then again it might
not. Also, reducing the size of your pool might keep individual sockets
more active.
before creating the pool this app ran quite happily creating sockets
as it recieved requests but the connection time was wasted time. the
only change i've made is the connection pool so i am reasonably
certain there isn't something in the rest of my code that is killing
connections.
has anybody got any idea's as to what could be killing my connections?
As I said before, very likely the remote end is probably closing the
connections after they have been idle long enough.
or where would be a good place to start looking?
It would have been well to actually read the API docs for Socket before
assuming that you knew what the timeout property did. Having not done
that, it would have been well to read them thoroughly once you started
having problems. It would have also been well to study a good resource
on network programming before diving in. Fortunately, it is not too late.
i didn't post any code up as i thought i'd poll for easy answers
before inflicting my code on you.
I hope mine were easy enough. It sounds to me like the problem may
indeed not be in your code at all, but instead in the assumptions behind it.
John Bollinger
(e-mail address removed)