Reputation: 705
I have a web application running on a Tomcat 6, installed on a Windows Server 2003. This application opens socket connections with at different server to retrieve some data. This application has been working for months. But in two occurrences for some reason the web application stopped working. In both occurrences running the netstat command (with -ano) showed about 4.000 tcp connections (on TIME_WAIT state) on the port that the web app is connecting through the socket connection.
The strange thing starts here: I stop tomcat, after a while those connections drop (running netstat again). I restart tomcat and these connections are all back on TIME_WAIT state!!!
I have no clue why these connections are reopened. It might be something very obvious that I am missing. Any ideas?
Thank you.
EDIT:
Here is the code that does the socket management:
public void openSocketConnection() throws Exception {
socket = createSingleSocket();
socket.setSoTimeout(5000);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
}
/**
* Closes socket and output input streams.
*/
public void closeSocketConnection() throws IOException {
socket.close();
out.close();
in.close();
}
/**
* Writes the input to the socket and returns the output response
*
* @param input
* a String to write on the socket
* @return output a String that the socket returns
* @throws Exception
*/
public String writeReadSocket(String input) throws Exception {
openSocketConnection();
out.println(input);
logger.debug("Socket Input:" + input);
String output = in.readLine();
logger.debug("Socket output:" + output);
closeSocketConnection();
return output;
}
/**
* Method overiden by Spring. We use the method injection technique, so that we have a new socket instance in every call to openSocketConnection()
* method
*/
public abstract Socket createSingleSocket();
The method that is called is writeReadSocket.
The last method (createSingleSocket) is used by spring, so I can have a new Socket instance for every call to openConnection() method (by the way, I am not sure if this is correct to have a new Socket for every request). In a Spring configuration file I have this:
<!-- prototype scope is used to instantiate a new socket instance in every createSingleSocket() method call. -->
<bean id="socket" class="java.net.Socket" scope="prototype">
<constructor-arg type="String" value="${socket.url}"/>
<constructor-arg type="int" value="${socket.port}"/>
</bean>
<bean id="socketConnector" class="gr.diassa.dsjsonrpcsuite.socket.SocketConnector">
<lookup-method name="createSingleSocket" bean="socket"/>
</bean>
Upvotes: 1
Views: 2281
Reputation: 17903
Please check couple of things
Upvotes: 2