I'm developing a simple webapplication in C#. It stores data in
multiple XML-files. When I post data to the ASP.NET page, several XML
files will be updated. But when it failes, some kind of exception, I
want to rollback all actions.
Is this possible in ASP.NET, just like database Transactions?
// do actions in several XML-files
// do rollback
// commit all changes to XML files on disk
In versions of Windows earlier than Vista, the filesystem is not
transactional, so you need a separate tool to do transactions on your files.
You could use Component Services (COM+) to implement a Compensating Resource
Manager (CRM). The CRM will provide the transaction log and will roll back
changes during a system restart if it crashed during the update of your
files, but you will have to provide the code (in your own DLL) to commit and
rollback the transation, typically by means of moving files in and out of a
temp folder. It can all be done in .Net by means of the
System.EnterpriseServices namespace. If I recall correctly, Microsoft
Official Course 2557 contains a chapter that teaches how to create a CRM,
and the example that they use is built precisely on changes to the
In newer versions of Windows, you can do transactional operations on
I have done the CRM with COM+, and it worked all right. I have not used
the KTM on NTFS.
> Or is it simpler to create some transaction-object and keep temp-files
> when changing...
Yes, this is simpler... but does not provide for automatic recovery when
rebooting after a system crash in the middle of a transaction.
The usual trick for that sort of things is to do actions on copies of
files (which you create before you start this process), and then, once
everything is done and no errors have occured, you rename the new
files on top of the old ones, overwriting the latter.
Note that this can still go wrong if you're dealing with more than one
file, and something bad happens between renaming of two files (so you
end up with some files updated with new stuff, and some files still
with old content). "Something bad" in this case being anything from an
exception (say, because some other process has removed one of the
"new" files), to a hard system reset. Of course, to properly deal with
the latter, you will need a properly journalled transacted storage
anyway (or will have to write one).