C# : Transactions and file-actions

68 views
Skip to first unread message

hoest

unread,
Mar 27, 2009, 6:28:57 AM3/27/09
to
Hi,

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?

try {
// do actions in several XML-files
}
catch(Exception){
// do rollback
}
finally {
// commit all changes to XML files on disk
}

Thanks!

Alberto Poblacion

unread,
Mar 27, 2009, 7:21:20 AM3/27/09
to
"hoest" <jel...@gmail.com> wrote in message
news:b5e1bc73-a501-48b5...@r33g2000yqn.googlegroups.com...

> 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?

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
filesystem.

In newer versions of Windows, you can do transactional operations on
NTFS:
http://msdn.microsoft.com/en-us/library/bb986748(VS.85).aspx
http://msdn.microsoft.com/en-us/magazine/cc163388.aspx
http://www.codeproject.com/KB/vista/VistaKTM.aspx

hoest

unread,
Mar 27, 2009, 10:24:36 AM3/27/09
to
On 27 mrt, 12:21, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.org> wrote:
> "hoest" <jell...@gmail.com> wrote in message

Do you have some code-samples? Ever tried it yourself?

Or is it simpler to create some transaction-object and keep temp-files
when changing...

Alberto Poblacion

unread,
Mar 27, 2009, 10:43:26 AM3/27/09
to
"hoest" <jel...@gmail.com> wrote in message
news:e9b95d09-5b79-4fa4...@y13g2000yqn.googlegroups.com...

> Do you have some code-samples? Ever tried it yourself?

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.

Pavel Minaev

unread,
Mar 27, 2009, 7:16:21 PM3/27/09
to

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).

Reply all
Reply to author
Forward
0 new messages