1

Javaプログラムには次のシナリオがあります。

...
try

   // 1
   PreparedStatement pstmt1 = conn.getPreparedStatement("SQL QUERY");
   pstmt1.setQueryTimeout(1);
   pstm.executeUpdate();
   System.out.println("1 executed");

   // 2
   PreparedStatement pstmt2 = conn.getPreparedStatement("SQL QUERY");
   pstmt2.setQueryTimeout(1);
   pstmt2.executeUpdate();
   System.out.println("2 executed");

   // 3
   PreparedStatement pstmt3 = conn.getPreparedStatement("SQL QUERY");
   pstmt3.setQueryTimeout(1);
   pstmt3.executeUpdate();
   System.out.println("3 executed");

catch(Exception e){

     e.printStackTrace();

}
...

「ケーブルを抜く」と、最初のexecuteUpdate()呼び出しの直後にデータベースへの接続が失われた場合。1秒間だけ待機し、応答がない場合はすぐにキャッチに入るようにプログラムに指示するにはどうすればよいですか?

ここで何が起こっているのかというと、プログラムはその時点(最初のexecuteUpdate()、出力「1execute」)の後でスタックします。

メソッドpstmt.setQueryTimeout(1)が機能していないようです。

サーバーの接続プールプロパティで接続タイムアウトを10秒に設定しました。

数分(30分)後、次のエラー(予想されるエラー)が発生します。

The Connection Manager received a fatal connection error from the Resource Adapter for resource jdbc/JNDI_BD1.  The exception which was received is com.ibm.websphere.ce.cm.StaleConnectionException: [jcc][t4][2030][11211][3.58.82] A communication error occurred during operations on the connection's underlying socket, socket input stream, or socket output stream.  Error location: Reply.fill().  Message: No route to host. ERRORCODE=-4499, SQLSTATE=08001:com.ibm.db2.jcc.am.io: [jcc][t4][2030][11211][3.58.82] A communication error occurred during operations on the connection's underlying socket, socket input stream, or socket output stream.  Error location: Reply.fill().  Message: No route to host. ERRORCODE=-4499, SQLSTATE=08001:java.net.SocketException: No route to host

どんな助けでも大歓迎です。

4

2 に答える 2

1

すべてのドライバーがクエリタイムアウトをサポートしているわけではありません。ただし、その場合でも、クエリタイムアウトはネットワークタイムアウトを検出することを目的としていません。クエリのタイムアウトなどのオプションは、データベースサーバーによって処理される可能性があります(つまり、ドライバーがサーバーに要求します。xxxより長くかかる場合はクエリを中止します)。または、ドライバーやサーバーがクエリをまったくサポートしていません。

ソケットタイムアウトははるかに低いレベルのタイムアウトであり、ほとんどのドライバーには、このための接続プロパティ設定(so_timeout、ソケットタイムアウトなど)があります。

于 2012-11-13T08:37:35.733 に答える
0

コードをにラップできる場合は、 guavaのTimeLimiter::callWithTimeoutCallableを使用することをお勧めします。それはまさにこの目的のために設計されています。

于 2012-11-12T22:56:47.487 に答える