diff --git a/src/main/java/org/mariadb/jdbc/client/impl/ConnectionHelper.java b/src/main/java/org/mariadb/jdbc/client/impl/ConnectionHelper.java index ec96f6eca..66a7e61ea 100644 --- a/src/main/java/org/mariadb/jdbc/client/impl/ConnectionHelper.java +++ b/src/main/java/org/mariadb/jdbc/client/impl/ConnectionHelper.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.lang.reflect.Constructor; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.sql.SQLException; @@ -109,11 +110,24 @@ public static Socket connectSocket(final Configuration conf, final HostAddress h socket = createSocket(conf, hostAddress); SocketHelper.setSocketOption(conf, socket); if (!socket.isConnected()) { - InetSocketAddress sockAddr = - hostAddress.pipe == null && hostAddress.localSocket == null - ? new InetSocketAddress(hostAddress.host, hostAddress.port) - : null; - socket.connect(sockAddr, conf.connectTimeout()); + boolean isRemoteSocket = hostAddress.pipe == null && hostAddress.localSocket == null; + if (!isRemoteSocket) { + socket.connect(null, conf.connectTimeout()); + } else { + InetAddress[] allAddress = InetAddress.getAllByName(hostAddress.host); + IOException lastException = null; + for (InetAddress address : allAddress) { + try { + socket.connect(new InetSocketAddress(address, hostAddress.port), conf.connectTimeout()); + break; + }catch (IOException ignore) { + lastException = ignore; + } + } + if (lastException != null) { + throw lastException; + } + } } return socket;