Solved

why the timeout never work in Java ?

Posted on 2002-05-18
16
177 Views
Last Modified: 2010-03-31
here is my code :

....
Socket ws = new Socket("192.168.3.1", 1433);
ws.setSoTimeout(5000);
.....

I believe I have set the timeout valur properly.
But why it does work ?

How to make it ready timeout in 5 seconds ????
0
Comment
Question by:j_script
16 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
That should set the timeout for a blocking read.
What are you expecting to timeout?
0
 

Author Comment

by:j_script
Comment Utility
I see. I want to detect whether a SQL Server is alive and
can be connected before doing jdbc.

And if someone shutdown the SQL server, then above class will
take time to know that SQL Server cannot be connected.

Do you have better idea ?

I want to know whether the SQL Server is alive within 8 seconds.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
AFAIK Java does not support this (though check 1.4, not sure if it's been added).
The only way I can think of would be to use a seperate thread to handle the timeout.
0
 

Author Comment

by:j_script
Comment Utility
how would I do that ?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
You'd close the connection if it hadn't connected in 5 seconds.
0
 
LVL 9

Expert Comment

by:Venci75
Comment Utility
as objects said - this is a wrong way to do this.
If you want to check whether the connection is still alive - you can send some data representing 'is alive?' network packages each 5 seconds. When there is an error in this communication - either the 'is alive?' data cannot be sent or the response of this package (which means 'yes - I am alive!') cannot be received - then the connection is broken.

... but I think that in this case the objects's suggestion is the better approach.
0
 

Author Comment

by:j_script
Comment Utility
objects,

So, I am think two threads. One thread use socket connect
to the server, and other thread will check a variable.
If that variable is null after 5 seconds, means the first
thread still try to connect to the server, then second
thread will kill first thread, right ?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Yes that's correct.
Though you may not be able to stop the thread making the connection because it is blocked on IO. If this is the case then you'll just need to inform whoever is waiting on the connection, that connection failed.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:j_script
Comment Utility
Do you have any thread code for that ?
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
Comment Utility
Try something like:

void connect()
{
   worker.start();

   syncronized(result)
   {
      try
      {
         // Wait until connection is successful,
         // or wait times out

         result.wait(timeout);
      }
      catch (Exception e)
      {
         System.out.println(e);
      }
   }
}

public void run()
{
   // Attempt the connection

   syncronized(result)
   {
      try
      {
         // notify the connect() method

         result.notify();
      }
      catch (Exception e)
      {
         System.out.println(e);
      }
   }
}
0
 

Author Comment

by:j_script
Comment Utility
Objects,

I will increase another 100 points for you.
Can you modify you code, so it is really only testing for
a connection to MS SQL Server ?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
The above code can be used to test for timeout of any type of connection. Do you have your current connection code.
0
 

Author Comment

by:j_script
Comment Utility
No, I have not. I do not know how to put them together.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Just add your connection code in the above where it says:

 // Attempt the connection

0
 

Author Comment

by:j_script
Comment Utility
objects,

I am very new to Java Thread. I have modified the code.
Please take a look :

public class TEST extends Thread{
  public static void main(String[] argc){
    TestConnect tc = new TestConnect();
    tc.start();
    syncronized(result){
      try{
        result.wait(8000);
      }
      catch(Exception e){
        System.out.println(e);
      }
    }
  }
}

class TestConnect extends Thread{
  public void run(){
    // My MS SQL Server Connection code here
    syncronized(result){
      try{
        result.notify();
      }
      catch(Exception e){
        System.out.println(e);
      }
    }
  }
}

I do not know if my modification is correct.
I want to wait for 8 seconds. If after 8 seconds, there is
still no connection, how do I kill the thread or maybe
print out whether the connection is good/bad ?
0
 
LVL 5

Expert Comment

by:Netminder
Comment Utility
Force-accepted.

Netminder
EE Admin
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now