I have software that has to write "packets" of goods to a database. It must write either all of them, or none, in case of an error. To do this we use transactions. Begin transactions, N-hundred inserts, commit transaction. So far so good.
Now there are two different scenarios:
there is a bug in a query for whatever reason or the database stops responding while inserting: the transaction is open, and requires a rollback.
the entire software crashes during the insert, and the transaction remains open.
In these cases I need to be able to recover. I was thinking to simply kill all rows that are returned by this query:
SELECT spid FROM sys.sysprocesses WHERE open_tran > 0;
But this kills the connection if the situation is the first one above. And a Rollback does not work if it the situation 2 above.
what would be the proper way to recover from the above situations, and be sure that there are no open transactions?