Solved

why the timeout never work in Java ?

Posted on 2002-05-18
16
179 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
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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

Accepted Solution

by:
objects earned 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…

895 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