I have a query where I'm getting column names from a DB. These column names won't change on a request or session basis. So, to avoid running useless queries on each request, I would like to run this query once when the application is deployed and then keep the results in a List<String>
for rest of the lifecycle of the application (until its deployed again).
How can this be achieved so that I can use the final List<String>
in my service layer?
If there is a better way to do this rather than tying running a query on deploy, then please suggest some other way.
Currently, I have this:
public List<String> fetchColNames (String tableName) {
String query = "SELECT Upper(column_name) FROM information_schema.columns WHERE table_name = ?";
List<String> cols = getJdbcTemplate().query(query, new Object[] {tableName}, new RowMapper<String>() {
public String mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getString(1);
}
});
return cols;
}