title: Cancel/Close long running namejdbctemplate
body: <p><strong>tl;dr:</strong></p>
  <p>I need option, to able to terminate long running (namedjdbctemplate) queries in my application from an other thread. Another word user able to stop a running application.</p>
  <pre><code>List&lt;List&lt;Object&gt;&gt; query = namedjdbctemplate.query(sql, argMap, anyresultSetMapper());
  </code></pre>
  <p>For Datasource, I am using <strong>HikariCP</strong>
  Namedjdbctemplate init: <code>NamedParameterJdbcTemplate njdbcTemplate = new NamedParameterJdbcTemplate(/*HikariDataSource*/ ds);</code></p>
  <p>Problem: In <strong>NamedParameterJdbcTemplate</strong> I can't reach the Connection object. The reason I need NamedParameterJdbcTemplate  because 99% of my queries has a lot of named parameters. (:name, :date etc)...</p>
  <p>I am also using many databases (Postgresql, Oracle, MySQl mainly and some others) so I cannot really change the parameters from :name -&gt; to @name cause its not compatible everywhere.</p>
  <p>I tried to get it from the other side. My plan was to get all active connection from the pool and find the connection based on the statement.</p>
  <pre><code>        java.lang.reflect.Field poolField = ds.getClass().getDeclaredField(&quot;pool&quot;);
          poolField.setAccessible(true);
          this.pool = (HikariPool) poolField.get(ds);
  
          System.out.println(&quot;Active: &quot; + pool.getActiveConnections());
          System.out.println(&quot;idle: &quot; + pool.getIdleConnections());
  
          
          java.lang.reflect.Field connectionBagField = pool.getClass().getDeclaredField(&quot;connectionBag&quot;);
          connectionBagField.setAccessible(true);
          ConcurrentBag&lt;PoolEntry&gt; /* error here*/ connectionBag = (ConcurrentBag&lt;PoolEntry&gt;) connectionBagField.get(pool);
          
  </code></pre>
  <p>The first part works well, but it can not resolve pool entry.</p>
  <p><strong>Other question</strong> let me assume I got the connection with your help. Is it a good idea to call connection.close() when I want to terminate an sql query?</p>
  <p>PS: I assume even if this code works with namedjdbctemplate I would get a  new connection not what I need.</p>
  <pre><code>    JdbcTemplate jdbcTemplate = new JdbcTemplate(primaryDataSource.getDs());
  
      Connection con;
      try {
          con = jdbcTemplate.getDataSource().getConnection();
          con.close();
      } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  </code></pre>
  <p>PS2:
  WIth this code, I have the connection, but here I can't use namedJdbcTemplate...</p>
  <pre><code>        String SQL_QUERY = &quot;select * from ecollect.debtor d limit 10&quot;;
      Connection connection = ds.getConnection();
  
      try (Connection con = connection;
              PreparedStatement pst = con.prepareStatement(SQL_QUERY);
              ResultSet rs = pst.executeQuery();) {
          while (rs.next()) {
              if (Math.random() &lt; 0.03) {
                  System.out.println(&quot;Hups&quot;);
                  connection.close();
                  taskStatus = TaskStatus.ERROR;
              }
              System.out.println(&quot;#&quot; + this.user + &quot;\t&quot; + rs.getString(&quot;debt_id&quot;));
          }
      }
  </code></pre>