How to cancel a long-running Database operation? How to cancel a long-running Database operation? oracle oracle

How to cancel a long-running Database operation?


If you're using ADO.NET and SQL data provider, take a look at SqlCommand.Cancel method. That does what you're looking for. However, it tries to cancel and the cancellation may take time. Basically, it's up to SQL Server to decide when to grant your cancellation request. When the query is cancelled, you should get a SqlException that indicates that the operation was cancelled by user. Apparently, you don't want to treat this exception as exception and handle it specially such as if SqlException is due to user cancelling the operation, just swallow it.


I also noticed command.Cancel() doesn't really abort the command. What worked for me is closing the connection (rollback transaction if you use one) when the user aborts. This will raise an exception in your background thread while the command is executing, so you have to catch it and check the CancellationPending property there and not rethrow the exception in that case...

// When abortingworker.CancelAsync();command.Connection.Close();// In your DoWork event handler...catch (Exception){    if (worker.CancellationPending)    {        e.Cancel = true;        return;    }    else    {        throw;    }}// And in your RunWorkerCompleted event handlerif (e.Error == null && !e.Cancelled){    ...}


I am pretty sure it is possible- we use TOAD for Oracle, and it lets you cancel long-running queries, as described here. I'm not sure how they do it though.