You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by rh...@apache.org on 2006/04/28 15:44:31 UTC
svn commit: r397899 - in
/db/derby/code/trunk/java/client/org/apache/derby/client/net: NetAgent.java
NetConnection40.java
Author: rhillegas
Date: Fri Apr 28 06:44:29 2006
New Revision: 397899
URL: http://svn.apache.org/viewcvs?rev=397899&view=rev
Log:
DERBY-1090: Commit Olav's client1090_patch2.diff patch, which addresses recovery from hanging servers.
Modified:
db/derby/code/trunk/java/client/org/apache/derby/client/net/NetAgent.java
db/derby/code/trunk/java/client/org/apache/derby/client/net/NetConnection40.java
Modified: db/derby/code/trunk/java/client/org/apache/derby/client/net/NetAgent.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/client/org/apache/derby/client/net/NetAgent.java?rev=397899&r1=397898&r2=397899&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/net/NetAgent.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/net/NetAgent.java Fri Apr 28 06:44:29 2006
@@ -20,10 +20,13 @@
package org.apache.derby.client.net;
+import java.net.SocketException;
+
import org.apache.derby.client.am.Agent;
import org.apache.derby.client.am.DisconnectException;
import org.apache.derby.client.am.SqlException;
import org.apache.derby.client.am.Utils;
+import org.apache.derby.shared.common.sanity.SanityManager;
public class NetAgent extends Agent {
//---------------------navigational members-----------------------------------
@@ -323,6 +326,60 @@
}
}
+ /**
+ * Specifies the maximum blocking time that should be used when sending
+ * and receiving messages. The timeout is implemented by using the the
+ * underlying socket implementation's timeout support.
+ *
+ * Note that the support for timeout on sockets is dependent on the OS
+ * implementation. For the same reason we ignore any exceptions thrown
+ * by the call to the socket layer.
+ *
+ * @param timeout The timeout value in seconds. A value of 0 corresponds to
+ * infinite timeout.
+ */
+ protected void setTimeout(int timeout) {
+ try {
+ // Sets a timeout on the socket
+ socket_.setSoTimeout(timeout * 1000); // convert to milliseconds
+ } catch (SocketException se) {
+ // Silently ignore any exceptions from the socket layer
+ if (SanityManager.DEBUG) {
+ System.out.println("NetAgent.setTimeout: ignoring exception: " +
+ se);
+ }
+ }
+ }
+
+ /**
+ * Returns the current timeout value that is set on the socket.
+ *
+ * Note that the support for timeout on sockets is dependent on the OS
+ * implementation. For the same reason we ignore any exceptions thrown
+ * by the call to the socket layer.
+ *
+ * @return The timeout value in seconds. A value of 0 corresponds to
+ * that no timeout is specified on the socket.
+ */
+ protected int getTimeout() {
+ int timeout = 0; // 0 is default timeout for sockets
+
+ // Read the timeout currently set on the socket
+ try {
+ timeout = socket_.getSoTimeout();
+ } catch (SocketException se) {
+ // Silently ignore any exceptions from the socket layer
+ if (SanityManager.DEBUG) {
+ System.out.println("NetAgent.getTimeout: ignoring exception: " +
+ se);
+ }
+ }
+
+ // Convert from milliseconds to seconds (note that this truncates
+ // the results towards zero but that should not be a problem).
+ timeout = timeout / 1000;
+ return timeout;
+ }
protected void sendRequest() throws DisconnectException {
try {
Modified: db/derby/code/trunk/java/client/org/apache/derby/client/net/NetConnection40.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/client/org/apache/derby/client/net/NetConnection40.java?rev=397899&r1=397898&r2=397899&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/net/NetConnection40.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/net/NetConnection40.java Fri Apr 28 06:44:29 2006
@@ -130,6 +130,14 @@
* The validity is checked by running a simple query against the
* database.
*
+ * The timeout specified by the caller is implemented as follows:
+ * On the server: uses the queryTimeout functionality to make the
+ * query time out on the server in case the server has problems or
+ * is highly loaded.
+ * On the client: uses a timeout on the socket to make sure that
+ * the client is not blocked forever in the cases where the server
+ * is "hanging" or not sending the reply.
+ *
* @param timeout The time in seconds to wait for the database
* operation used to validate the connection to complete. If the
* timeout period expires before the operation completes, this
@@ -155,6 +163,12 @@
// Do a simple query against the database
synchronized(this) {
try {
+ // Save the current network timeout value
+ int oldTimeout = netAgent_.getTimeout();
+
+ // Set the required timeout value on the network connection
+ netAgent_.setTimeout(timeout);
+
// If this is the first time this method is called on this
// connection we prepare the query
if (isValidStmt == null) {
@@ -167,6 +181,9 @@
// Run the query against the database
ResultSet rs = isValidStmt.executeQuery();
rs.close();
+
+ // Restore the previous timeout value
+ netAgent_.setTimeout(oldTimeout);
} catch(SQLException e) {
// If an SQL exception is thrown the connection is not valid,
// we ignore the exception and return false.