Solved

uncaught SocketTimeoutException issue

Posted on 2011-09-23
23
298 Views
Last Modified: 2012-05-12
Hello,

Here is a sample code snippet:

                    try {
                        getModel().refreshFilters();
                        success = true;
                    } catch (IOException ioe) {
                        handleException(getView(), ioe, "Problem refreshing filter data.");
                    }

When running this code, my application throws an exception:

Caused by: java.net.SocketTimeoutException: Read timed out
      at com.gepower.d400.communication.SyncRequest.execute(SyncRequest.java:149)
      at com.gepower.d400.communication.SyncRequest.execute(SyncRequest.java:143)
      at com.gepower.d400.communication.SyncRequest.execute(SyncRequest.java:111)

Shouldn't the SocketTimeoutException which is a sub-class of IOException be caught ?

Paolo
0
Comment
Question by:ptreves
  • 10
  • 9
  • 4
23 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36587591
Are you sure it comes from this very place?
No methods refreshfiletern getMode are metioned.$
Add some printout ion the truy catch loop befiore calling method nd after caling
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36591532
Please post full output
0
 

Author Comment

by:ptreves
ID: 36598621
Here is the complete stack trace:

java.io.IOException: java.net.SocketTimeoutException: Read timed out
java.io.IOException: java.net.SocketTimeoutException: Read timed out
      at com.gepower.d400.communication.database.RecordDataFilterLoader.populateRecordFilterManager(RecordDataFilterLoader.java:59)
      at com.gepower.d400.model.database.RecordFilterManager.refresh(RecordFilterManager.java:68)
      at com.gepower.d400.view.runtime.database.ReportModel.refreshFilters(ReportModel.java:493)
      at com.gepower.d400.view.runtime.database.ReportController$RefreshFiltersAction$1.doLongOperation(ReportController.java:307)
      at com.gepower.d400.swing.BusyThread.run(BusyThread.java:103)
Caused by: com.gepower.d400.communication.RequestException: java.net.SocketTimeoutException: Read timed out
      at com.gepower.d400.communication.SyncRequest.execute(SyncRequest.java:149)
      at com.gepower.d400.communication.database.RecordDataFilterLoader.populateRecordFilterManager(RecordDataFilterLoader.java:57)
      ... 4 more
Caused by: java.net.SocketTimeoutException: Read timed out
      at sun.reflect.GeneratedConstructorAccessor38.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
      at java.net.HttpURLConnection.getResponseCode(Unknown Source)
      at com.gepower.d400.communication.SyncRequest.execute(SyncRequest.java:143)
      ... 5 more
Caused by: java.net.SocketTimeoutException: Read timed out
      at java.net.SocketInputStream.socketRead0(Native Method)
      at java.net.SocketInputStream.read(Unknown Source)
      at java.io.BufferedInputStream.fill(Unknown Source)
      at java.io.BufferedInputStream.read1(Unknown Source)
      at java.io.BufferedInputStream.read(Unknown Source)
      at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
      at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
      at java.net.HttpURLConnection.getResponseCode(Unknown Source)
      at com.gepower.d400.communication.SyncRequest.execute(SyncRequest.java:111)
      ... 5 more
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 86

Expert Comment

by:CEHJ
ID: 36598646
>>Shouldn't the SocketTimeoutException which is a sub-class of IOException be caught ?

The result you're expecting will only happen if  no exceptions in threads other than the one executing your code occur. It's possibly fortunate for you, since you now have a proper stack trace to help you diagnose the actual cause of the problem, which could possibly be due to a network issue
0
 

Author Comment

by:ptreves
ID: 36598866
The problem occurs randomly after a series of "Clear Logs" / "Refresh Filters" commands.
I get the impression that the exception is not caught for some reason.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36599103
>>I get the impression that the exception is not caught for some reason.

See my first sentence in http:#36598646
0
 

Author Comment

by:ptreves
ID: 36601779
Here is what I think the problem could be:

Both RequestException and ResponseException extend directly from the Exception calls. Here is an example for the Request Exception :

public class RequestException extends Exception{
    public RequestException(String message){
        super(message);
    }

    public RequestException(Exception e){
        super(e);        
    }
}

Here is a relationship hierarchy of the different exceptions:
Exception - RequestException
Exception - ResponseException
Exception - IOException - SocketTimeoutException

Looking through the code based on the StackTrace, I noted the following code snippet:
from line 111 in SyncRequest. java:

            int statusCode = connection.getResponseCode();

From line 143 in SyncRequest.java (connection.getResponseCode()):
            try {
                if (connection != null && connection.getResponseCode() != -1) {
                    throw new ResponseException(connection.getResponseCode(), getErrorMessage(connection), errorBody );
                } else {
                    throw new RequestException(e);
                }
            } catch (IOException ioe) {
                throw new RequestException(ioe);
            }


It seems that the method calls:

            int statusCode = connection.getResponseCode();

are throwing the exception. The execute method signature is the following:

    public void execute() throws RequestException, ResponseException {

All method calls to:             int statusCode = connection.getResponseCode();
are wrapped aroung and IOException in a catch block which you would expect to catch any sub-exception like
SocketTimeoutException

Maybe that would be the fix ?

Paolo
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36601924

I understand that this exception
Caused by: com.gepower.d400.communication.RequestException: java.net.SocketTimeoutException: Read timed out

would not be caught by catch(IOException) because RequestException does not inherit from IOException

but I still don't understand why this exception

java.io.IOException: java.net.SocketTimeoutException: Read timed out

would not be caught?

When you are writing your own exceptions

RequestException and ResponseException - can't you extend these from IOExceptiion rather than Exception ?



0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36602228
Could you please post the full code for the method handleException?
0
 

Author Comment

by:ptreves
ID: 36709102


    /**
     * Shows a modal <code>ErrorDialog</code> with the Application Error title over the specified parent
     * with the specified message, and exception.
     * <p/>
     * This method is thread safe, although most Swing methods are not.
     *
     * @param parent    - parent to display dialog over
     * @param exception - exception to show stack of
     * @param message   - message to display to user
     */
    public void handleException(Component parent, Throwable exception, String message) {
        ErrorDialog.showErrorDialog(parent, exception, message);
    }

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36709409
That's slightly strange. Normally exceptions should be ... exceptional, so it's not usual to present them to the user in a dialog. You should really log them, with full stack trace.
Just to recap, is the stack trace at http:#36598621 the one that occurs?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36709565
>>so it's not usual to present them to the user in a dialog.

Of course the word 'not' shouldn't be present in the above ;)
0
 

Author Comment

by:ptreves
ID: 36710035
yes, that is the stack trace generated.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36711208
OK - forgive me for asking more questions, but i feel there's a lack of clarity here: do i take it that your goal is to solve the problem that's causing this exception?
0
 

Author Comment

by:ptreves
ID: 36712121
yes, absolutely.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36712537
It looks like either

a. an issue with the network (eliminate that by attempting to connect to different hosts)
b. an issue with that particular server (see a.)
0
 

Author Comment

by:ptreves
ID: 36712800
I extended RequestException and ResponseException both from IOException instead of Exception.
I still got this Stack Trace:

java.io.IOException: java.net.SocketTimeoutException: Read timed out
java.io.IOException: java.net.SocketTimeoutException: Read timed out
      at com.gepower.d400.communication.database.RecordDataFilterLoader.populateRecordFilterManager(RecordDataFilterLoader.java:59)
      at com.gepower.d400.model.database.RecordFilterManager.refresh(RecordFilterManager.java:68)
      at com.gepower.d400.view.runtime.database.ReportModel.refreshFilters(ReportModel.java:493)
      at com.gepower.d400.view.runtime.database.ReportController$RefreshFiltersAction$1.doLongOperation(ReportController.java:309)
      at com.gepower.d400.swing.BusyThread.run(BusyThread.java:103)
Caused by: com.gepower.d400.communication.RequestException: java.net.SocketTimeoutException: Read timed out
      at com.gepower.d400.communication.SyncRequest.execute(SyncRequest.java:149)
      at com.gepower.d400.communication.database.RecordDataFilterLoader.populateRecordFilterManager(RecordDataFilterLoader.java:57)
      ... 4 more
Caused by: java.net.SocketTimeoutException: Read timed out
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
      at java.net.HttpURLConnection.getResponseCode(Unknown Source)
      at com.gepower.d400.communication.SyncRequest.execute(SyncRequest.java:143)
      ... 5 more
Caused by: java.net.SocketTimeoutException: Read timed out
      at java.net.SocketInputStream.socketRead0(Native Method)
      at java.net.SocketInputStream.read(Unknown Source)
      at java.io.BufferedInputStream.fill(Unknown Source)
      at java.io.BufferedInputStream.read1(Unknown Source)
      at java.io.BufferedInputStream.read(Unknown Source)
      at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
      at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
      at java.net.HttpURLConnection.getResponseCode(Unknown Source)
      at com.gepower.d400.communication.SyncRequest.execute(SyncRequest.java:111)
      ... 5 more

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36712826
Yes, the reason for the exception, and its cure, are nothing to do with the type of your Exception
0
 

Author Comment

by:ptreves
ID: 36713105
This bug occurs periodially when pressing a compination of the command buttons "Refresh Filters" and "Clear Logs" in rapid succession over and over again.

Maybe introducing a delay in the Command Thread such as Thread.sleep(2000); or something would allow proper packet transfer and avoid any such Exceptions ?

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36713153
Maybe you could increase the timeout time?

Are you connecting in your own code - in this case you can use connect with timeout paparmeter method:
http://download.oracle.com/javase/1.4.2/docs/api/java/net/Socket.html#connect%28java.net.SocketAddress,%20int%29
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 36713362
>>Maybe introducing a delay in the Command Thread such as Thread.sleep(2000) ...

Yes, that's a possibility. Some code simply should not be executed too much in quick succession
0
 

Author Comment

by:ptreves
ID: 36718630
There is a parameter called: SO_TIMEOUT (or DEFAULT_READ_TIMEOUT)
It's value is set to: 10000.
Another parmeter called READ_TIMEOUT=60000

I adjusted the values to 30000 and 40/60000 respectivily. with some improvements.
After numerous attemps, other kinds of exceptions get generated.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36718656
>other kinds of exceptions get generated

You mean not timeout, but other kinds of exceptions ?
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 107
reverse digits of a number using for loop 5 42
even odd program using while loop 3 41
MySQL  on Tomcat 8 46
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question