title: Spring DAO - first query with JdbcTemplate takes much longer time
body: <p>In my application I use Spring MVC with DAO pattern to access MSSql database with JdbcTemplate. Everything is working fine but I have noticed that first query takes a bit longer then next queries in each single request.</p>
  
  <p>I wrote a simple test:  </p>
  
  <pre><code>String sql = "SELECT 1";
  for (int i = 0; i &lt; 5; i++) {
      long startTime = System.currentTimeMillis();
      jdbcTemplate.queryForList(sql);
      logger.debug("query took: " + TimeHelper.showDuration(startTime));
  }
  </code></pre>
  
  <p>the result is:</p>
  
  <pre><code>2012-08-28 12:55:07,665 | Start
  2012-08-28 12:55:08,878 | query took: 424 milliseconds
  2012-08-28 12:55:08,893 | query took: 15 milliseconds
  2012-08-28 12:55:08,908 | query took: 14 milliseconds
  2012-08-28 12:55:08,922 | query took: 14 milliseconds
  2012-08-28 12:55:08,937 | query took: 14 milliseconds
  </code></pre>
  
  <p>I understand that there are some activities that Spring is doing to get all beans ready. But if there is request to server that executes only 1 query then it takes 424 miliseconds on each request.</p>
  
  <p>My question is: Is this behavior OK? Or there can be some bug in my code? Does it work so that on each request Spring needs this time for initialization? (and all other queries in request will run fast then)? Or I can do some startup initialization and then during each request the first query will take 15 ms too?</p>
  
  <p>My configuration is folowing:  </p>
  
  <pre><code>datasource: class="org.apache.commons.dbcp.BasicDataSource"
  </code></pre>
  
  <p>dao is injected to test class with:  </p>
  
  <pre><code>@Resource(name="testDao")  
  private TestDao testDao;  
  </code></pre>
  
  <p>I use annotations based approach. Test class and DAO doesn't use @Transaction.  </p>
  
  <p>EDIT:
  I also tried to configure initialSize but this didn't help, (with initialSize=2 it takes 800ms, with initialSize=1 it takes 400ms - each request).  </p>
  
  <p>I don't understand why it takes 400ms on each request when it is possible to have it for 15 ms. It seems like some overhead with Spring. :(</p>