Is there a way to cancel the copying process started by calling copyIn()
method in a separate thread?
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.
Killing threads does not work - COPY just keeps running.
The FutureTask<>
class is used to create the threads, so there is a list of them - one for each csv.
Calling task.cancel(true)
makes nothing in terms of copying process on the server. Only System.exit()
can kill it with fire.
Any ideas?
Some of my code:
Uploader.java implements Callable
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);
}
Upload files method body
for (File file : files) {
FutureTask<Long> ftask =
new FutureTask<>(
new Uploader(defaultTableName, file)
);
tasks.add(ftask);
execService.execute(ftask);
}
SOLVED:
The solution was found, however it required some changes in code.
Upload files method body
looks like this now
for (File file : files) {
Uploader uploader = new Uploader(defaultTableName, file);
uploaders.add(uploader);
Future<Long> f = execService.submit(uploader);
//save the Future to get the copy result when finished
}
Having this, we can easily call some Uploader
's method where it is possible to just close the database connection and handle the exception properly. It will stop copying on the server.
I accept that the solution might not be the most elegant one, however it works, it works fast, and not much code is needed.