Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Socket instead of SocketChannel when multiSubnetFailover=true #662

Merged
merged 1 commit into from
Apr 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
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