--- old/src/share/classes/com/sun/jndi/ldap/Connection.java 2013-12-03 18:32:04.443214111 +0400 +++ new/src/share/classes/com/sun/jndi/ldap/Connection.java 2013-12-03 18:32:03.875221253 +0400 @@ -439,32 +439,35 @@ BerDecoder readReply(LdapRequest ldr) throws IOException, NamingException { BerDecoder rber; - boolean waited = false; + int timeOut = (readTimeout > 0) ? + readTimeout : 15 * 1000; // Default read timeout of 15 sec - while (((rber = ldr.getReplyBer()) == null) && !waited) { + long startTime = System.currentTimeMillis(); + + while (((rber = ldr.getReplyBer()) == null) && timeOut > 0) { try { // If socket closed, don't even try synchronized (this) { if (sock == null) { throw new ServiceUnavailableException(host + ":" + port + - "; socket closed"); + "; socket closed"); } } - synchronized (ldr) { - // check if condition has changed since our last check - rber = ldr.getReplyBer(); - if (rber == null) { - if (readTimeout > 0) { // Socket read timeout is specified - - // will be woken up before readTimeout only if reply is - // available - ldr.wait(readTimeout); - waited = true; + long currTime = System.currentTimeMillis(); + if (startTime > currTime) { + // System time must have changed + startTime = currTime; + } + if ((timeOut -= (currTime - startTime)) > 0) { + startTime = currTime; + synchronized (ldr) { + // check if condition has changed since our last check + rber = ldr.getReplyBer(); + if (rber == null) { + ldr.wait(timeOut); } else { - ldr.wait(15 * 1000); // 15 second timeout + break; } - } else { - break; } } } catch (InterruptedException ex) { @@ -473,7 +476,7 @@ } } - if ((rber == null) && waited) { + if ((rber == null) && readTimeout > 0) { removeRequest(ldr); throw new NamingException("LDAP response read timed out, timeout used:" + readTimeout + "ms." );