C# : Transactions and file-actions

H

hoest

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!
 
C

Ciaran O''Donnell

There is no transacted file system in Windows as of yet so you will need to
come up with your own way to perform this.
One suggested is:
Hold different versions of the files in different files eg when
myfileA.version1.xml is updated, save the changed copy as myfileA.version2.xml
Have a file which holds the names of the latest in use version call index.xml.
Before updating the files, get an exlcusive lock on index.xml
update all the files you need and record the new file names you have given
them
update the index.xml and save that, then release the lock.

Always ensure your code releases the lock on index.xml, even when an
exception occurs. You can clean up non active versions periodically.

Thats just one idea.
 
A

Alberto Poblacion

hoest said:
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
 
H

hoest

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

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

Alberto Poblacion

hoest said:
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.
 
P

Pavel Minaev

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!

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top