title: How to reuse the same connection with a Spring's JdbcTemplate?
body: <p>I have the following code:</p>
  
  <pre>
  <code>
      @Test
      public void springTest() throws SQLException{
          //Connect to the DB.
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName("org.h2.Driver");
          dataSource.setUrl("jdbc:h2:/data/h2/testa");
          dataSource.setUsername("");
          dataSource.setPassword("");
          JdbcTemplate jt=new JdbcTemplate(dataSource);
          jt.execute("SELECT 1");
          jt.execute("SELECT 1");
      }
  </code>
  </pre>
  
  <p>I expect the two execute() lines to reuse the same connection.  However, the log output says:</p>
  
  <pre>
  2011-02-10 12:24:17 DriverManagerDataSource [INFO] Loaded JDBC driver: org.h2.Driver
  2011-02-10 12:24:17 JdbcTemplate [DEBUG] Executing SQL statement [SELECT 1]
  2011-02-10 12:24:17 DataSourceUtils [DEBUG] Fetching JDBC Connection from DataSource
  2011-02-10 12:24:17 DriverManagerDataSource [DEBUG] Creating new JDBC DriverManager Connection to [jdbc:h2:/data/h2/testa]
  2011-02-10 12:24:17 DataSourceUtils [DEBUG] Returning JDBC Connection to DataSource
  2011-02-10 12:24:17 JdbcTemplate [DEBUG] Executing SQL statement [SELECT 1]
  2011-02-10 12:24:17 DataSourceUtils [DEBUG] Fetching JDBC Connection from DataSource
  2011-02-10 12:24:17 DriverManagerDataSource [DEBUG] Creating new JDBC DriverManager Connection to [jdbc:h2:/data/h2/testa]
  2011-02-10 12:24:17 DataSourceUtils [DEBUG] Returning JDBC Connection to DataSource
  </pre>
  
  <p>The above example runs quite fast but I have a larger piece of code that does basically the same thing and hangs for a long time on <code>Creating new JDBC DriverManager Connection</code>.  I never get an error but it makes the code run very slowly.  Can I somehow refactor the above code to just use the same connection?</p>
  
  <p>Thanks</p>