0

What happens if I call Thread.Abort() (in C#/.NET) on a thread that is currently executing an ODBC Command (specifically against MSSQL and Oracle, but also generally)? Will the command get cancelled? Will the DB server recognize there's nothing at the other end of the connection and kill the process (again, specifically MSSQL and Oracle)? Or do I need to explicitly call Cancel() on the connection first?

My goal is to ensure the safety of the database I'm connecting to if the worst should happen to my application (or the worst that I can catch and respond to, like system shutdowns etc).

I'd like to program defensively and try to issue a Cancel() if at all possible, but I'd like to know the behavior anyway.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
JH147
  • 1
  • 1
    I think you have to call cancel – Collin Jun 16 '15 at 07:03
  • 1
    `Thread.Abort()` should work only when the code being executed is managed... so in truth it depends on how the driver is built, if it is managed code or it is unmanaged code. If the driver is unmanaged code then it is immune to `Thread.Abort()` (see for example http://stackoverflow.com/q/2781484/613130) – xanatos Jun 16 '15 at 07:08
  • See for example http://stackoverflow.com/q/1401532/613130 for a persons that tried `Thread.Abort()` vs `SqlDataAdapter` (hint: the `SqlDataAdapter` won :-) ) – xanatos Jun 16 '15 at 07:11
  • I guess to be more specific, I'm wondering if the .NET ODBCConnection class is smart enough to catch ThreadAbortExceptions and call its own Cancel() method before the exception is rethrown? – JH147 Jun 16 '15 at 07:31

1 Answers1

0

If you want to ensure cancelling the SQL command, why not to use the TransactionScope.Dispose() method or simply do not Complete the transaction? It works above Thread, Process and such abstractions, and there will be no races between Thread cancelling and SQL command.

Also, as was stated in comments, your SQL driver can work in other Thread and can even be an unmanaged code, so the cancelling the Thread will not affect the SQL command, and you really need to Cancel() your connection or command.

VMAtm
  • 27,943
  • 17
  • 79
  • 125