I want to detect when a database is down and I know once the database is down it will throw a SQLException. Using Connection.isValid(). What does the timeout paramter represent? It says:
timeout - - The time in seconds to wait for the database operation
used to validate the connection to complete. If the timeout period
expires before the operation completes, this method returns false. A
value of 0 indicates a timeout is not applied to the database
operation.
What does this mean?
The isValid() method allows you to check the Connection for validity.
For this method you can specify a timeout in seconds, when this timeout runs out before the internal validity check is finished, the method will return false.
This is useful if you don't want your program to wait a long time in case the validity check takes too long.
If you give 0 as input for timeout, no timeout will be applied and your program will wait until the check is complete.
Well, "database is down" means it does not respond. It's not like it can just tell you "hey, I'm down". So the timeout means how long does this method wait for response, before considering database as offline.
It represent the time to discard the connection if any connection is idle for that much time.
Related
I have been trying to understand the difference between apicallattempttimeout and apicalltimeout. What I could understand is apicalltimeout is the total time till the client request waits for the response before giving up whereas apicallattemptimeout includes timeout for retries as well in addition to the time in the first attempt.
So does this mean that apicallattemptimeout will always be more than apicalltimeout? Example : Suppose I keep apicalltimeout to be 1000ms and for a single retry I want the timeout to be 300ms. So values will be for apicalltimeout= 1000ms and apicallattemptimeout= 1300ms ? API docs dont seem to help here
apicallattempttimeout and apicalltimeout
Right now I have two threads running in my program. One constantly tries to read input from the user, and the other watches for a timeout. A timeout occurs if the user does not send any input in a given amount of time. The two threads look like this:
User input thread
while(true){
if(in.hasNextLine()){
processLine(in.nextLine());
timeLastRecieved = System.currentTimeMillis();
}
}
Timeout thread
while(true){
//Check for a timout
if(timeLastRecieved+timeoutDuration <= System.currentTimeMillis())
timeUserOut();
else{
//Sleep until it is possible for a timeout to occur
Thread.sleep((timeLastSent+timeoutDuration) - System.currentTimeMillis());
}
}
As of now I have these thread separated, but I could combine them like this...
while(true){
if(in.hasNextLine()){
processLine(in.nextLine());
timeLastRecieved = System.currentTimeMillis();
}
//Check for a timout
if(timeLastRecieved+timeoutDuration <= System.currentTimeMillis())
timeUserOut();
}
But I really don't need to check for a timeout that frequently. So should I combine the threads and check for a timeout too often, or should I have two threads. I am not as worried about performance as I am proper coding etiquette. If it means anything the timeout duration in something like 15 minutes long.
EDIT: Just want to point out that in the version with two thread I am sleeping, but in the combined version I never sleep the thread. This obviously causes the if statement that checks for a timeout to run more then necessary.
To summarize my comments: I don't think a separate thread to check for timeouts is necessary.
Reasons:
You'd need to share information like timeLastRecieved between them, which could be more complex than wanted (e.g. AFAIK in some cases access to long values is not atomic).
From your description it seems that polling for user input and timeout (no input provided in time) are closely related, thus the polling thread could check for the timeout as well. That doesn't mean it has to handle the timeout too, just reporting it somewhere or calling some timeout handler might be better design.
It is easier to read and understand since updating timeLastRecieved and checking for a timeout is handled in the same place.
Since there is no inter-thread communication nor coordination needed (there are no threads that need to communicate) it probably is more robust as well.
A few hints on checking for the timeout:
You should calculate the timeout threshold when you update timeLastReceived and then only check agains the current time instead of calculating it in every iteration.
You might want to calculate the timeout threshold before processing the input in order not to have it depend on the processing time as well.
Finally, there are alternative approaches like using java.util.Timer. Here you could simply schedule a timeout task which is executed when the timeout should occur. That task then would check if the timeout really happened and if not it just returns.
To handle new input before the timeout occured you could use at least two approaches:
Cancel the current timeout task, remove it from the timer and schedule a new one.
If there is already a scheduled timeout task then don't schedule a new one but wait for the current one to run. The current one then checks for the timeout and if none happened it schedules a new task (or itself) for the current anticipated timeout (note that this would require some inter-thread communcation so be careful here).
You need to have two threads - one waiting for data coming in through the InputStream / Reader, and one that's watching the time to see if the time elapsed as taken too long. The only way to do it with 1 thread would be to sleep for a segment of the timeout period and then poll for data periodically. But that's less efficient than having a separate thread dedicated to reading from your InputStream/Reader.
You may want to check out Timeout as a generic option for implementing a timeout
According to the java docs of connection.setReadTimeout() -
"A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout."
So if I set a certain read timeout, and start downloading a file, will the download break if the timeout is reached?
Or does it break only when there is nothing to read for that specified timeout?
It's only raise exception if there is no response. It'll not break while getting data so don't worry about.
If you start downloading the file and there is no interruption, the timeout will not occur.
If you have the timeout set to 30 seconds. If you're downloading for 15 seconds and after those 15 seconds, the connection fails, you will have to wait another 30 seconds before timeout.
The read timeout starts afresh every time the recv() function is called.
So the correct answer is (2).
In my current project, it will receive some messages from upstream systems, and upload them to another storage server by http service concurrently.
Since the system may receive many messages from upstream system at a short time, I use apache HttpClient with a pool, and, set:
If http client can't connect to storage server in 10s, it will throw exception
If http connection can't receive response from storage server in 10s, it will throw exception
If system can't get http connection from pool in 30 seconds, it will throw exception.
But my friend disagrees the 3rd point. She says, if the new messages can't get connections from the pool, just let them wait, since they will get a connection finally and save to storage server. The exceptions are not necessary in this case.
But I'm afraid that if we received too many messages from upstream, that there will be too many threads are blocking to wait connections, this may result the system unstable.
Do you think point 3 is good or bad? Do I need to set a timeout for it?
If system can't get http connection from pool in 30 seconds, it will throw exception.
Do you think point 3 is good or bad? Do I need to set a timeout for it?
This seems very much to be a business decision and not a coding issue. Is it okay for the request to wait for a long time for things? Is it okay for the storage interface to throw an exception if some time expires?
If the storage-server is somehow hosed I'm assuming that all of the requests (that are able to get a connection) are waiting 10 seconds and then throwing. If you have enough connections in the queue then this may cause your persist operations to wait a long time to even get a connection. Seems like a timeout is warranted but again this a business decision.
Generally, I would provide a timeout parameter (in seconds or millis) for the persist method to wait to complete. Then the caller can pass in Long.MAX_VALUE if they want otherwise they will get an exception. Or have another method that does not have a timeout parameter that is documented to chain to the other method with Long.MAX_VALUE.
I trying to make a call to a very heavy duty process.
It's average work length is estimated by 9-10 minutes.
When I'm executing the process, I set the timeout for a ridiculously huge number: 99999999.
After 2 minutes, I get the following error:
java.net.SocketTimeoutException: Read timed out
I tried to mess with it some more, and I set the timeout to 3000, and after 3 seconds as anticipated I got the same error.
Do you have any idea on why socket.setSoTimeout(99999999) sets it to 120000 max?
I had the same problem and the solution was not use
socket.shutdownInput(); socket.shutDownOutput(); until the last time of reading or writing data to the socket. This made the socket go to FIN_WAIT state thus waiting 2 minutes before closing. You can read more about it in this post
Clearly you aren't setting the timeout you think you're setting, or someone else is changing it afterwards. You'll have to post some code to get further elucidation.
Note that according to W.R. Stevens in TCP/IP Illustrated, Vol II, #17.4, the timeout is held in a short as a number of 1000Hz ticks, so a timeout beyond 11 minutes isn't possible. This applies to the BSD code.
I'm not sure how your application works, but try to set an infinite timeout to the socket
public void setSoTimeout(int timeout)
throws SocketException
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.
If you provide more information about your call, i may improve the answer.