|
From: Colin S. <col...@ex...> - 2004-02-09 19:47:58
|
This reminds me of something else in JdbcTemplate that I've been meaning
to bring up for a while. Why does the JdbcTemplate(DataSource)
constructor eagerly instantiate the SQL exception translator? I seem to
remember it was not always so.
As per the email below, anywhere you don't know for sure you are
threadsafe, it's convenient to use JdbcTemplate in a form like
new JdbcTemplate(datasource).xxxxx(...);
but this results in an annoying lookup of the metadata to build the
exception translator.
Now obviously you could instead just use a static factory method which
creates an instance for you like this:
public static JDBCTemplate createJdbcTemplate(Datasource ds) {
JdbcTemplate jt = new JdbcTemplate();
jt.setDataSource(ds);
return jt;
}
but I'm not sure why the eager loading should be the default. And even
the above static method is 'technically' wrong, since it shold by
contract be calling afterPropertiesSet(), which would force the eager
init of the exception translator.
Aside from making it non-eager, the other option is for the exception
translator to actually cache lookups towards the same datasource...
Regards,
Colin
Colin Sampaleanu wrote:
> I've seen somebody get burned by the fact that HibernateTemplate is
> threadsafe, while JdbcTemplate is not. We probably need to document
> this difference a bit better, and the probable usage scenario that
> results (i.e. it's generally ok to produce one singleton
> HibernateTemplate in your context and use it everywhere, whereas you
> probably want to create jdbcTemplates on demand; not even setting it
> non-singleton in the context is enough, since if it is fed as a
> dependency to another object which is singleton, and that object is
> assumed to be thread safe, there will be problems).
>
> Will add some stuff to the javadocs tonight...
|