Skip to content

Commit

Permalink
[CONJ-1187] throw an explicit type SQLTimeoutException in place of SQ…
Browse files Browse the repository at this point in the history
…LNonTransientConnectionException when connection fail because of timeout
  • Loading branch information
rusher committed Oct 10, 2024
1 parent 8741f85 commit 3df9916
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import java.lang.reflect.Constructor;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.sql.SQLException;
import java.sql.SQLNonTransientConnectionException;
import java.sql.SQLTimeoutException;
import java.util.Arrays;
import java.util.List;
import javax.net.SocketFactory;
Expand Down Expand Up @@ -117,7 +119,14 @@ public static Socket connectSocket(final Configuration conf, final HostAddress h
}
return socket;

} catch (SocketTimeoutException ste) {
throw new SQLTimeoutException(
String.format("Socket timeout when connecting to %s. %s", hostAddress, ste.getMessage()),
"08000",
ste);

} catch (IOException ioe) {

throw new SQLNonTransientConnectionException(
String.format("Socket fail to connect to %s. %s", hostAddress, ioe.getMessage()),
"08000",
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLNonTransientConnectionException;
import java.sql.SQLTimeoutException;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.ZoneId;
Expand Down Expand Up @@ -243,17 +244,21 @@ public StandardClient(
postConnectionQueries();
}
setSocketTimeout(conf.socketTimeout());

} catch (IOException ioException) {
destroySocket();

String errorMsg =
String.format("Could not connect to %s : %s", hostAddress, ioException.getMessage());

throw exceptionFactory.create(errorMsg, "08000", ioException);
} catch (SQLException sqlException) {
destroySocket();
throw sqlException;
} catch (SocketTimeoutException ste) {
destroySocket();
throw new SQLTimeoutException(
String.format("Socket timeout when connecting to %s. %s", hostAddress, ste.getMessage()),
"08000",
ste);
} catch (IOException ioException) {
destroySocket();
throw exceptionFactory.create(
String.format("Could not connect to %s : %s", hostAddress, ioException.getMessage()),
"08000",
ioException);
}
}

Expand Down

0 comments on commit 3df9916

Please sign in to comment.