Is there a way to set the query timeout in milliseconds, instead of seconds? The java.sql.Statement API only has a method for seconds, but even 1 second is too slow for my use case.
Java API: http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#setQueryTimeout(int)
I am using the Oracle DB.
Query time-out is different from connection time-out or login time-out. The connection or login timeout occurs when the initial connection to the database server reaches a predefined time-out period. At this stage, no query has been submitted to the server.
The first approach sets query timeout for the specific query. The next method sets query timeout for all queries within a given SQL session. The last query sets the timeout for all queries in all sessions. You can use any of these methods as per your requirement.
If you want to set session wide or global timeout, then you need to use the following SQL queries. Please note, the timeout values are in milliseconds. The first query below sets session-wide query timeout while the second query sets the global timeout. SET SESSION MAX_EXECUTION_TIME=2000; SET GLOBAL MAX_EXECUTION_TIME=2000;
The timeout period elapsed prior to completion of the operation or the server is not responding. These errors occur on the application side. The application sets a time out value and if the time out is reached, it cancels the query.
This is quite convoluted, but could work:
Future<ResultType>
will have a "get(Long, TimeUnit)" function, blocking until at most the set timeout - which has configurable granularity (at least it promises to be like that...Something like this in almost-Java-code:
public class MyCallable implements Callable<ResultType> {
@Override
public ResultType call() throws Exception {
//do query, with 1s timeout - that way no thread will be busy for more than 1s
return result;
}
}
Handling the requests
ExecutorService threadPool; //initialised somewhere earlier
Future<ResultType> futureResult = threadPool.submit(new MyCallable());
try {
ResultType result = futureResult.get(100, TimeUnit.MILLISECONDS);
//results received in time, do the processing here
} catch(TimeoutException e) {
//request too slow -- handle it
} catch( other exceptions...) { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With