title: Spring JDBC template : queryforlist doesn't close the db connection?
body: <p>Having background jobs in a wildfly web application where <code>NamedParameterJdbcTemplate</code> is used to retrieve a list of records, I've a database connection leak preventing the jobs to work after a while, <code>CannotGetJdbcConnectionException</code>. </p>
  
  <p>After monitoring a part of the  sql requests, here is what i found. For this simple piece of code : </p>
  
  <pre><code>String sqlselectreq = 
      "SELECT * " + 
      "FROM systemedpdfdb.documents " +
      "WHERE state=-3 AND closing_date IS null";
  
  NamedParameterJdbcTemplate namedParameterJdbcTemplate = new  
      NamedParameterJdbcTemplate(this.getDataSource());
  
  Map&lt;String, Object&gt; namedParameters = new HashMap&lt;String, Object&gt;();
  // namedParameters.put("param_name_here", param_value_here);
  List&lt;Map&lt;String, Object&gt;&gt; queryres =  
      namedParameterJdbcTemplate.queryForList(sqlselectreq, namedParameters);
  
  if (!(queryres == null || queryres.isEmpty())) {
      for (Map&lt;String, Object&gt; queryrec : queryres) {
         ....
      }
  } else {
      return new Result(this, Result.CodeCategory.NOERROR, 
          Result.Type.TEXT, "NOTHING TO DO".getBytes(), ident);
  
  }
  </code></pre>
  
  <p>I have the following traces where we can see a useless last getConnection without close.</p>
  
  <pre><code>2017-05-11 15:37:00,031 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [DataSource] getConnection()
  2017-05-11 15:37:00,281 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [Connection] prepareStatement(SELECT * FROM systemedpdfdb.documents WHERE state=-3 and closing_date is null)
  2017-05-11 15:37:00,297 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [PreparedStatement] executeQuery()
  2017-05-11 15:37:00,312 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [ResultSet] next()
  2017-05-11 15:37:00,312 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [ResultSet] close()
  2017-05-11 15:37:00,312 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [PreparedStatement] isClosed()
  2017-05-11 15:37:00,328 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [PreparedStatement] close()
  2017-05-11 15:37:00,328 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [Connection] isClosed()
  2017-05-11 15:37:00,328 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [Connection] close()
  2017-05-11 15:37:00,328 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [DataSource] getConnection()
  2017-05-11 15:37:00,328 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [Connection] getMetaData()
  </code></pre>
  
  <p>Is it normal ? Or do I misuse Spring Jdbc Template ?</p>
  
  <p>Thanks in advance </p>
  
  <p>David L.</p>