I have a Java library where I access the DB via JDBC using Spring’s JDBC support. This library contains approximately a DAO class for each table I need to access, which are over a hundred. Currently I instantiate a new JdbcTemplate, or one of its variants, each time I need to perform a new query. Is this considered good practice or should I reuse a single JdbcTemplate as much as I can? I’ve actually seen examples of both approaches in either books or online documentation.
The context is a J2EE application, but ideally the code should be usable in different contexts, e.g. in offline tests or command line support tools.
Inject one, why bother instantiating? (It’s unclear if you mean “instantiate through the Spring context” or “instantiate using
new“.)Most samples I’ve seen do this in the config, I’m not even sure I’ve seen them instantiated manually outside of demo/test code. I see little benefit to doing it manualy, and zero if done outside of Spring.