Skip to content

Instantly share code, notes, and snippets.

@kcleereman
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kcleereman/48d3bfaa4a6599160867 to your computer and use it in GitHub Desktop.
Save kcleereman/48d3bfaa4a6599160867 to your computer and use it in GitHub Desktop.
CancellableStatement
public class CancellableStatement {
private static final Tuple nullTuple = new Tuple(null, null);
private final ExecutorService priorityExecutorService;
private final AtomicReference<Tuple> reference = new AtomicReference<>(nullTuple);
private final Promise<String> cancellationWatcher;
private static class Tuple {
private final OracleConnection oracleConnection;
private final CallableStatement callableStatement;
public Tuple(OracleConnection oracleConnection, CallableStatement callableStatement) {
this.oracleConnection = oracleConnection;
this.callableStatement = callableStatement;
}
}
public CancellableStatement(ExecutorService priorityExecutorService, Promise<String> cancellationWatcher) {
this.priorityExecutorService = priorityExecutorService;
this.cancellationWatcher = cancellationWatcher;
}
/*
* Cancel/abort the statement/connection
*/
public void cancel() {
Tuple tuple = reference.getAndSet(null);
try {
if(tuple != null && tuple.callableStatement != null) {
tuple.callableStatement.cancel();
tuple.callableStatement.close();
}
} catch(Throwable t) {
listener.cancellationFailure(t);
}
try {
if(tuple != null && tuple.oracleConnection != null) {
tuple.oracleConnection.abort(priorityExecutorService);
}
} catch(Throwable t) {
listener.cancellationFailure(t);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment