File System Transactions using .NET on WinXP
I'm using WinXP on clients and Win2003 on a server.
I need do atomic actions: create-moving files, insert-update database.
Are there any good practices for file system transactions using WinXP? I know in Vista/Win2008/Win7 there are TxF transaction (NTFS) but not in WinXP.
I don't want use COM+, neither other complex solutions. Only need good sample code, for good practices.
Transactions and file-actions by Alberto Poblacion
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://codeproject.com/KB/vista/VistaKTM.aspx
Edit.
References:
https://transactionalfilemgr.codeplex.com/
http://www.codeproject.com/Articles/690136/All-About-TransactionScope
http://ayende.com/blog/4528/who-stole-my-transaction
http://www.chinhdo.com/20080825/transactional-file-manager/
http://bmegias.wordpress.com/2010/10/25/ejecutar-acciones-al-finalizar-la-transaccion-transactioncompleted-vs-enlistvolatile/
You could create your own class that implements IEnlistmentNotification.
Here's an example of someone that did: http://www.chinhdo.com/20080825/transactional-file-manager/
You might want to look at the File.Replace method which I think uses transactions since it requires NTFS. http://msdn.microsoft.com/en-us/library/9d9h163f(v=vs.100).aspx
You won't get true filesystem transactions for NTFS in XP. That said, you may not need it.
For example, with software installation, you can largely get the semantics of transactions for free by using something like Windows Installer.
What is it you are looking to ultimately accomplish?
The .NET Transactional File Manager should work under XP.
Out of interest, TxF is likely to be pulled from future releases of Windows (certain features have been deprecated in Win 8).
See How to write a transaction to cover Moving a file and Inserting record in database?.
精彩评论