The Problem
My programm has an operation which consists of a series of single operations that all change global state. Each of the elementary operations can fail and leave the global state in an undefined state.
The Question
Is there a generic pattern that helps me to make the composed operation "atomic" in the sense that if one of the suboperations fails, the global state is left unchanged?
I use C++, so if answers contain code, please prefer that language if you have a choice. But I do not mind examples in other languages.
Comments
-
This resembles the "atomicity" of a database, where you add all or nothing when doing a commit. How is that implemented?
-
In my case my global state is the state of the file-system. I need to add or remove multiple files at once and want to make sure that they are all added or nothing is added.
My Thoughts
The best I can come up with is a class that gets a list of operations and their inverse operations. If any of the operations fails, it executes the reverse operations of the already executed operations. But what to do if one of the reverse operations fails?
How would the interface of that class look like? Would I have an extra class that resembles an reversible operation? Do you know a place where I can read more about this problem?
Aucun commentaire:
Enregistrer un commentaire