Skip to content

Commit

Permalink
Fix microsoft#1964 by not having a static field at all. It was cleare…
Browse files Browse the repository at this point in the history
…d each time anyway.
  • Loading branch information
Milan Mimica committed Nov 11, 2022
1 parent 81395b1 commit a9b907c
Showing 1 changed file with 32 additions and 41 deletions.
73 changes: 32 additions & 41 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -2340,10 +2341,6 @@ enum Result {
// necessary for raising exceptions so that the connection pool can be notified
private final SQLServerConnection conn;

// list of addresses for ip selection by type preference
private static ArrayList<InetAddress> addressList = new ArrayList<>();
private static final Lock addressListLock = new ReentrantLock();

/**
* Constructs a new SocketFinder object with appropriate traceId
*
Expand Down Expand Up @@ -2680,7 +2677,9 @@ private SocketFactory getSocketFactory() throws IOException {

/**
* Helper function which traverses through list of InetAddresses to find a resolved one
*
*
* @param addressList
*
* @param hostName
*
* @param portNumber
Expand All @@ -2689,20 +2688,15 @@ private SocketFactory getSocketFactory() throws IOException {
* @throws IOException
* @throws SQLServerException
*/
private InetSocketAddress getInetAddressByIPPreference(String hostName,
int portNumber) throws IOException, SQLServerException {
addressListLock.lock();
try {
InetSocketAddress addr = InetSocketAddress.createUnresolved(hostName, portNumber);
for (int i = 0; i < addressList.size(); i++) {
addr = new InetSocketAddress(addressList.get(i), portNumber);
if (!addr.isUnresolved())
return addr;
}
return addr;
} finally {
addressListLock.unlock();
private InetSocketAddress getInetAddressByIPPreference(List<InetAddress> addressList, String hostName,
int portNumber) throws IOException, SQLServerException {
InetSocketAddress addr = InetSocketAddress.createUnresolved(hostName, portNumber);
for (int i = 0; i < addressList.size(); i++) {
addr = new InetSocketAddress(addressList.get(i), portNumber);
if (!addr.isUnresolved())
return addr;
}
return addr;
}

/**
Expand All @@ -2723,30 +2717,31 @@ private InetSocketAddress getInetAddressByIPPreference(String hostName,
private Socket getSocketByIPPreference(String hostName, int portNumber, int timeoutInMilliSeconds,
String iPAddressPreference) throws IOException, SQLServerException {
InetSocketAddress addr = null;
List<InetAddress> preferredAddresses;
InetAddress addresses[] = InetAddress.getAllByName(hostName);
IPAddressPreference pref = IPAddressPreference.valueOfString(iPAddressPreference);
switch (pref) {
case IPv6First:
// Try to connect to first choice of IP address type
fillAddressList(addresses, true);
addr = getInetAddressByIPPreference(hostName, portNumber);
preferredAddresses = fillAddressList(addresses, true);
addr = getInetAddressByIPPreference(preferredAddresses, hostName, portNumber);
if (!addr.isUnresolved())
return getConnectedSocket(addr, timeoutInMilliSeconds);
// No unresolved addresses of preferred type, try the other
fillAddressList(addresses, false);
addr = getInetAddressByIPPreference(hostName, portNumber);
addr = getInetAddressByIPPreference(preferredAddresses, hostName, portNumber);
if (!addr.isUnresolved())
return getConnectedSocket(addr, timeoutInMilliSeconds);
break;
case IPv4First:
// Try to connect to first choice of IP address type
fillAddressList(addresses, false);
addr = getInetAddressByIPPreference(hostName, portNumber);
preferredAddresses = fillAddressList(addresses, false);
addr = getInetAddressByIPPreference(preferredAddresses, hostName, portNumber);
if (!addr.isUnresolved())
return getConnectedSocket(addr, timeoutInMilliSeconds);
// No unresolved addresses of preferred type, try the other
fillAddressList(addresses, true);
addr = getInetAddressByIPPreference(hostName, portNumber);
addr = getInetAddressByIPPreference(preferredAddresses, hostName, portNumber);
if (!addr.isUnresolved())
return getConnectedSocket(addr, timeoutInMilliSeconds);
break;
Expand Down Expand Up @@ -2777,33 +2772,29 @@ private Socket getSocketByIPPreference(String hostName, int portNumber, int time
}

/**
* Fills static array of IP Addresses with addresses of the preferred protocol version.
* Returns a list of IP Addresses with addresses of the preferred protocol version.
*
* @param addresses
* Array of all addresses
* @param ipv6first
* Boolean switch for IPv6 first
*/
private void fillAddressList(InetAddress[] addresses, boolean ipv6first) {
addressListLock.lock();
try {
addressList.clear();
if (ipv6first) {
for (InetAddress addr : addresses) {
if (addr instanceof Inet6Address) {
addressList.add(addr);
}
private List<InetAddress> fillAddressList(InetAddress[] addresses, boolean ipv6first) {
List<InetAddress> addressList = new ArrayList<>();
if (ipv6first) {
for (InetAddress addr : addresses) {
if (addr instanceof Inet6Address) {
addressList.add(addr);
}
} else {
for (InetAddress addr : addresses) {
if (addr instanceof Inet4Address) {
addressList.add(addr);
}
}
} else {
for (InetAddress addr : addresses) {
if (addr instanceof Inet4Address) {
addressList.add(addr);
}
}
} finally {
addressListLock.unlock();
}
return addressList;
}

private Socket getConnectedSocket(InetAddress inetAddr, int portNumber,
Expand Down

0 comments on commit a9b907c

Please sign in to comment.