Suppose I have a application that handles money transfers.
try {
transaction.Commit();
} catch {
transaction.Rollback();
}
Now what happens, if it turns out that the Commit has to be canceled, and for some reason the program crashes before the cancel or rollback is handled?
How can I program my application so that it will be robust against program crashes.
EDIT:
What if I don’t have an SQL backend, and the transaction is something I myself have implemented?
Your design is fine (even if very minimalist with no real example in it…)
you can imagine that once you call
var transaction = connection.BeginTransaction();if you do not call commit the transaction will not get committed so the catch will roll it back.
if for any reason a crash prevents execution of the catch block auto rollback will be applied by the SQL engine.