title: Multiple JdbcTemplate instances or not?
body: <p>From what I understand, both <code>DataSource</code> and <code>JdbcTemplates</code> are <code>threadsafe</code>, so <em>you can configure a single instance of a <code>JdbcTemplate</code> and then safely inject this shared reference into multiple DAOs (or repositories)</em>. Also <code>DataSource</code>should be a Spring singleton, since it manages the connection pool.</p>
  
  <p>The official <a href="http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/jdbc.html#jdbc-JdbcTemplate-idioms" rel="noreferrer">Spring Documentation JdbcTemplate best practices</a> explains the alternatives (excerpts from the manual are in italics, and my notes between square brackets:</p>
  
  <ul>
  <li><em>configure a DataSource in your Spring configuration file, and then dependency-inject that shared DataSource bean into your DAO classes; the JdbcTemplate is created in the setter for the DataSource.</em> [with XML configuration and this leads to multiple JdbcTemplate instances, since in the datasource setter there is <code>new JdbcTemplate(dataSource)</code>]</li>
  <li><em>use component-scanning and annotation support for dependency injection. In this case you annotate the class with @Repository (which makes it a candidate for component-scanning) and annotate the DataSource setter method with @Autowired.</em> [also this case leads to multiple JdbcTemplate instances]</li>
  <li><em>If you are using Spring's JdbcDaoSupport class, and your various JDBC-backed DAO classes extend from it, then your sub-class inherits a setDataSource(..) method from the JdbcDaoSupport class. You can choose whether to inherit from this class. The JdbcDaoSupport class is provided as a convenience only.</em> [since you've an instance of JdbcDaoSupport for each class extending it, there is an instance of JdbcTemplate too for each instance of the derived class (see <a href="https://github.com/spring-projects/spring-framework/blob/master/spring-jdbc/src/main/java/org/springframework/jdbc/core/support/JdbcDaoSupport.java" rel="noreferrer">source code for JdbcDaoSupport</a>)]</li>
  </ul>
  
  <p>However, a later note, discourages all the options just presented:</p>
  
  <blockquote>
    <p>Once configured, a JdbcTemplate instance is threadsafe. You may want multiple JdbcTemplate instances if your application accesses multiple databases, which requires multiple DataSources, and subsequently multiple differently configured JdbcTemplates.</p>
  </blockquote>
  
  <p>In other words, all the options just presented will result in having multiple JdbcTemplate instances (one per DAO), and just after the docs says that is not necessary when working with a single database. </p>
  
  <p>What I would do is inject directly <code>JdbcTemplate</code> to the various DAOs needing it, so my question is, is it OK to do so? And also, do you also think that the Spring reference documentation is self-contradicting? Or is my misunderstanding?</p>