SingleConnectionDataSource dataSource = new SingleConnectionDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://".concat(user.getIpAddr()).concat(":").concat(user.getMysqlPort())); dataSource.setUsername(user.getMysqlLogin()); dataSource.setPassword(user.getMysqlPasswd()); jdbcTemplate = new JdbcTemplate(dataSource); 

then I execute the query:

 extens = jdbcTemplate.query(query, ROW_MAPPER_EXTEN); 

so, if the remote server does not respond at all (well, for example, an error in the IP address), then I need to set a timeout for such a case.

I tried:

 try { dataSource.setLoginTimeout(100); } catch (SQLException se) { log.warn(se.toString()); } 

But in the end an exception is generated:

java.lang.UnsupportedOperationException: setLoginTimeout

    1 answer 1

    Try adding a parameter to the URL

    connectTimeout = TIME_IN_SEC

    https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html#option_mysql_connect_timeout

    • Thank! Exactly what is needed. - Vitaly M.