I have the following scenario in a Java program:
...
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();
}
...
If I “unplug the cable” and the connection to the database is lost just after the first executeUpdate() call. How can I tell the program to wait only for 1 second and if no response go into the catch immediately?
Now what is happening is that the program get stuck after that point (the first executeUpdate(), at the output “1 executed”).
The method pstmt.setQueryTimeout(1) seems to be not working.
I’ve set the connection timeout in 10 seconds on the connection pools properties of the server.
After a lot of minutes (half an hour) I get the following error (the expected error):
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
Any help is very appreciated.
Not all drivers support query timeouts. But even if they did: the query timeout is not intended to detect network timeouts. Most likely options like query timeouts will be handled by the database server (ie: the driver asking the server: abort the query if it takes longer than xxx), or the driver and/or server don’t support it at all.
The socket timeout is a much lower level timeout, and most drivers will have a connection property setting for this (so_timeout, socket timeout etc).