Skip to content

Commit

Permalink
Use Socket instead of SocketChannel when multiSubnetFailover=true (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulvii committed Apr 5, 2018
1 parent 4ca55d9 commit 07e391e
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Expand Up @@ -2337,8 +2337,8 @@ else if (!useTnir) {
findSocketUsingJavaNIO(inetAddrs, portNumber, timeoutInMilliSeconds);
}
else {
LinkedList<Inet4Address> inet4Addrs = new LinkedList<>();
LinkedList<Inet6Address> inet6Addrs = new LinkedList<>();
LinkedList<InetAddress> inet4Addrs = new LinkedList<>();
LinkedList<InetAddress> inet6Addrs = new LinkedList<>();

for (InetAddress inetAddr : inetAddrs) {
if (inetAddr instanceof Inet4Address) {
Expand All @@ -2360,11 +2360,10 @@ else if (!useTnir) {

if (!inet4Addrs.isEmpty()) {
if (logger.isLoggable(Level.FINER)) {
logger.finer(this.toString() + "Using Java NIO with timeout:" + timeoutForEachIPAddressType);
logger.finer(this.toString() + "Using Java Threading with timeout:" + timeoutForEachIPAddressType);
}

// inet4Addrs.toArray(new InetAddress[0]) is java style of converting a linked list to an array of reqd size
findSocketUsingJavaNIO(inet4Addrs.toArray(new InetAddress[0]), portNumber, timeoutForEachIPAddressType);
findSocketUsingThreading(inet4Addrs, portNumber, timeoutForEachIPAddressType);
}

if (!result.equals(Result.SUCCESS)) {
Expand Down Expand Up @@ -2590,16 +2589,12 @@ private void findSocketUsingJavaNIO(InetAddress[] inetAddrs,

// if a channel was selected, make the necessary updates
if (selectedChannel != null) {
// the selectedChannel has the address that is connected successfully
// convert it to a java.net.Socket object with the address
SocketAddress iadd = selectedChannel.getRemoteAddress();
selectedSocket = new Socket();
selectedSocket.connect(iadd);
// Note that this must be done after selector is closed. Otherwise,
// we would get an illegalBlockingMode exception at run time.
selectedChannel.configureBlocking(true);
selectedSocket = selectedChannel.socket();

result = Result.SUCCESS;

// close the channel since it is not used anymore
selectedChannel.close();
}
}

Expand Down Expand Up @@ -2638,7 +2633,7 @@ private Socket getConnectedSocket(InetSocketAddress addr,
return selectedSocket;
}

private void findSocketUsingThreading(LinkedList<Inet6Address> inetAddrs,
private void findSocketUsingThreading(LinkedList<InetAddress> inetAddrs,
int portNumber,
int timeoutInMilliSeconds) throws IOException, InterruptedException {
assert timeoutInMilliSeconds != 0 : "The timeout cannot be zero";
Expand Down Expand Up @@ -2722,6 +2717,10 @@ private void findSocketUsingThreading(LinkedList<Inet6Address> inetAddrs,
}
}
}

if (selectedSocket != null) {
result = Result.SUCCESS;
}
}

/**
Expand Down

0 comments on commit 07e391e

Please sign in to comment.