I'm using NamedParameterJdbcTemplate
. There are three methods, each do almost the same, except different SQL queries, like:
public open fun method1() : String {
val sql = "select * from func1()"
return namedParameterJdbcTemplate.queryForObject(sql, new MapSqlParameterSource(), String::class.java)
}
public open fun method2() : String {
val sql = "select * from func2()"
return namedParameterJdbcTemplate.queryForObject(sql, new MapSqlParameterSource(), String::class.java)
}
public open fun method3() : String {
val sql = "select * from func3()"
return namedParameterJdbcTemplate.queryForObject(sql, new MapSqlParameterSource(), String::class.java)
}
I need to set different timeouts for executing queries: first method 10s, second 5s, etc
What is the best way to do this?
I know there is setQueryTimeout thing, but it sets timeout for jdbctemplate and every query will use same timeout.
I have two approaches:
I don't know what to choose.
Maybe there are another options.
I think the best way to achieve this is to create a separate JdbcTemplate for each query
For example create method:
private fun createNamedJdbcTemplateWithTimeOut(timeoutSeconds: Int): NamedParameterJdbcTemplate {
val template = JdbcTemplate(//your datasource//);
template.queryTimeout = timeoutSeconds;
return NamedParameterJdbcTemplate(template)
}
And now in your repository/service class you can use it like this:
private val jdbcTemplateWithTimeout1: NamedParameterJdbcTemplate = createNamedJdbcTemplateWithTimeOut(1)
private val jdbcTemplateWithTimeout10: NamedParameterJdbcTemplate = createNamedJdbcTemplateWithTimeOut(10)
private val jdbcTemplateWithTimeout30: NamedParameterJdbcTemplate = createNamedJdbcTemplateWithTimeOut(30)
You can use it in your methods just as regular JdbcTemplate