Is there a way to implement something like COMMIT transactions of RDBMSs in R in order to ensure a “all-or-nothing” paradigm (e.g. only save a file to a hard drive if storing the file path in a table in the RDBMS was successful as well)?
EDIT 2011-10-27
I was asked to be more specific:
Suppose you have a function that performs a number of tasks: send a http request, communicate status information to a master process, save an rdata file in some directory, alter some tables in a db etc. I would like to prevent only a partial “success” of that function, e.g. file being saved but db tables not updated, which could easily happen when the function crashes before completing the db part. Basically, my use cases is all sorts of data consistency stuff.
Hope that makes it more comprehensive.
Thats a bit tricky because its hard to know how to undo all the things you might have done that need undoing. With an RDBMS, the TRANSACTION START puts a little marker in the DB file, and then if anything fails it knows where to go back to, until the COMMIT comes and then everything changed from the TRANSACTION START to the COMMIT are written in stone (this explanation massively simplified).
The only thing I can think to help you is the “try” functions, which will catch errors. It’ll then be your code’s job to work out how much it managed to do and how to undo it before proceeding.
If you put things in functions it’s possible to use this kind of pattern:
[yeah thats not even R syntax but you should get the picture]
and then you do:
and you can be sure that foo won’t change if something goes wrong in updatefoo….