Java – would it be better to instantiate a new jdbctemplate in each query or inject a new jdbctemplate anywhere?
I have a Java library. I use spring's JDBC support to access the database through JDBC This library contains about 100 Dao classes for each table I need to access Currently, every time I need to execute a new query, I instantiate a new jdbctemplate or one of its variants Is this considered a good practice or should I reuse as many individual jdbctemplates as possible? I've actually seen examples of these two methods in books or online documents
Context is a J2EE application, but ideally, the code should be available in different contexts, such as offline testing or command line support tools
Solution
Inject one, why instantiate it? (it's not clear whether you mean "instantiate through spring context" or "instantiate with new")
Most of the samples I've seen do this in configuration, and I'm not even sure if I instantiate them manually outside of the demo / test code I think it's good to do this. It's zero if it's done outside spring