I'm working on a Java app that can be used connected to my office network or not.
My problem is that, when the user is not connected, the DriverManager.getConnection()
takes up to 20 seconds before finally releasing that it can't connect to the database.
Code that connect me or not to the server :
public static Connection getConnection() {
try{
//cnx is my Connection object
if(cnx ==null || cnx.isClosed()){
Class.forName("com.mysql.jdbc.Driver");
//Get the connection if possible
cnx = DriverManager.getConnection(connectionString, user, password);
}
/*
Basically, I'm just checking if "ConfigUser.isConnected()" is true
later in the code to know if I should try to execute SQL queries
*/
ConfigUser.setConnected(true);
return cnx;
}
catch (Exception e){
}
ConfigUser.setConnected(false);
return null;
}
I've tried to use the DriverManager.setLoginTimeout()
function but it didn't seem to change anything.