Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set query timeout in milliseconds, not seconds, on invocation of PreparedStatement?

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.

like image 932
Marcus Avatar asked Sep 19 '13 15:09

Marcus


People also ask

What is the difference between query time out and connection time out?

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.

How to set query timeout for all queries in SQL 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.

How to set session-wide or global timeout in SQL Server?

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;

What is timeout error in SQL?

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.


1 Answers

This is quite convoluted, but could work:

  1. have an ExecutorService
  2. create a Callable for the query
  3. submit the callable to ExecutorService
  4. the 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...) { ... }

Concerns:

  • I don't know how much overhead this means...
  • timeout: I don't know how exactly will they be handled.
  • the requests that time out, will be stuck in the threadpool until the inner, JDBC timeout (1s) kicks in...
  • threadpool: if fixed, might be a bottleneck (see concern above), if dynamic: might be an overhead
like image 108
ppeterka Avatar answered Sep 24 '22 22:09

ppeterka