?
Solved

why the timeout never work in Java ?

Posted on 2002-05-18
16
Medium Priority
?
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
16 Comments
 
LVL 92

Expert Comment

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

Author Comment

by:j_script
ID: 7019050
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
ID: 7019069
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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:j_script
ID: 7019423
how would I do that ?
0
 
LVL 92

Expert Comment

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

Expert Comment

by:Venci75
ID: 7021255
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
ID: 7021372
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
ID: 7022718
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
 

Author Comment

by:j_script
ID: 7082895
Do you have any thread code for that ?
0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 7082994
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
ID: 7092398
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
ID: 7094128
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
ID: 7094777
No, I have not. I do not know how to put them together.
0
 
LVL 92

Expert Comment

by:objects
ID: 7094789
Just add your connection code in the above where it says:

 // Attempt the connection

0
 

Author Comment

by:j_script
ID: 7094837
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
ID: 8581100
Force-accepted.

Netminder
EE Admin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

801 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