title: How to abort JDBC Postgresql CopyManager copying?
body: <p>Is there a way to cancel the copying process started by calling <code>copyIn()</code> method in a separate thread?</p>
  
  <p>Say, I have a list of csv-files which I need to copy from, getting maximum of database server power. So I create n-threads-connections for n-files, but I cannot find a way to abort a single operation if, for example, a wrong file has been chosen.</p>
  
  <p>Killing threads does not work - COPY just keeps running.</p>
  
  <p>The <code>FutureTask&lt;&gt;</code> class is used to create the threads, so there is a list of them - one for each csv.</p>
  
  <p>Calling <code>task.cancel(true)</code> makes nothing in terms of copying process on the server. Only <code>System.exit()</code> can kill it with fire.</p>
  
  <p>Any ideas?</p>
  
  <p>Some of my code:</p>
  
  <p><code>Uploader.java implements Callable</code></p>
  
  <pre class="lang-java prettyprint-override"><code>public static long uploadFile(final File file, final String tableName) {
  
      long status = 0;
      try {
          CopyManager copyManager = 
             new CopyManager((BaseConnection) new DataSource().connect());
          FileReader reader = new FileReader(file);
          status = copyManager.copyIn(sql, reader);
      } catch (SQLException | IOException e) {
         ...
      }
      return status;
  }
  
  @Override
  public Long call() throws Exception {
      return uploadFile(file, tableName);
  }
  </code></pre>
  
  <p><code>Upload files method body</code></p>
  
  <pre class="lang-java prettyprint-override"><code>for (File file : files) {
          FutureTask&lt;Long&gt; ftask =
                  new FutureTask&lt;&gt;(
                          new Uploader(defaultTableName, file)
                  );
          tasks.add(ftask);
          execService.execute(ftask);
      }
  </code></pre>
  
  <p><strong>SOLVED:</strong></p>
  
  <p>The solution was found, however it required some changes in code.</p>
  
  <p><code>Upload files method body</code> looks like this now</p>
  
  <pre class="lang-java prettyprint-override"><code>for (File file : files) {
      Uploader uploader = new Uploader(defaultTableName, file);
      uploaders.add(uploader);
      Future&lt;Long&gt; f = execService.submit(uploader);
  
      //save the Future to get the copy result when finished
  
  }
  </code></pre>
  
  <p>Having this, we can easily call some <code>Uploader</code>'s method where it is possible to just close the database connection and handle the exception properly. It will stop copying on the server. </p>
  
  <p>I accept that the solution might not be the most elegant one, however it works, it works fast, and not much code is needed.</p>