简体   繁体   中英

Application crashes and transactions in .NET

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?

Data persistence in Db would be achieved only after the Commit is successfull. So if your Commit fails in any position, the data would not be written to db.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM