-
-
Save kcleereman/48d3bfaa4a6599160867 to your computer and use it in GitHub Desktop.
CancellableStatement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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