Rod,
Yes I know you mention JdbcTemplate is thread-safe in both documentation and your books (at least the first one, I recently bought the second one but still waiting for delivery, so don't know for sure).
I've read the source code of JdbcTemplate and now I think I understand why the sample code is thread safe. However I still feel like the sample code is a little bit misguiding for beginners. For instance, consider how such a programmer could "extend" the code:
Code:
public int getCount() {
jt = new JdbcTemplate(dataSource);
jt.setFetchSize(1);
int count = jt.queryForInt("select count(*) from mytable");
return count;
}
public List getList() {
jt = new JdbcTemplate(dataSource);
jt.setFetchSize(1000);
List rows = jt.queryForList("select * from mytable");
return rows;
}
Now the above code may still function but there would be no guarantees as to what fetch size will be used in queryForInt() and queryForList() in a multi-threaded scenario.
Since changing the above code to use:
Code:
JdbcTemplate jt = new JdbcTemplate(dataSource);
...
would solve the issue, my question is what does it buy to have jt as a member variable? Please forgive if I am clueless. I am just trying to understand.
Thank you in advance,
F.