6

クエリのタイムアウトを秒単位ではなくミリ秒単位で設定する方法はありますか? java.sql.Statement API には秒単位のメソッドしかありませんが、私のユース ケースでは 1 秒でも遅すぎます。

Java API: http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#setQueryTimeout(int)

Oracle DB を使用しています。

4

3 に答える 3

3

これは非常に複雑ですが、うまくいく可能性があります:

  1. ExecutorService がある
  2. クエリの Callable を作成する
  3. callable を ExecutorService に送信する
  4. これFuture<ResultType>には「get(Long, TimeUnit)」関数があり、設定されたタイムアウトまでブロックします-これは設定可能な粒度を持っています(少なくともそのようになることを約束します...

ほぼJavaコードで次のようなもの:

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;
  }
}

リクエストの処理

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...) { ... }

懸念:

  • これがどれだけのオーバーヘッドを意味するのかわかりません...
  • timeout: それらがどのように処理されるか正確にはわかりません。
  • タイムアウトしたリクエストは、内部の JDBC タイムアウト (1 秒) が開始されるまでスレッドプールにスタックされます...
  • threadpool: 修正された場合、ボトルネックになる可能性があります (上記の懸念事項を参照)。動的な場合: オーバーヘッドになる可能性があります
于 2013-09-19T16:18:18.203 に答える