Using Transactional NTFS (TxF) with SQL Server in .NET

I just stumbled across the very new Transactional NTFS (TxF) .NET managed wrapper by Pietro Partescano and gave it a try.

TxF offers transactional protection for NTFS operations:

  • Atomic operations on single files.
    Makes writing files more robust.
  • Transactions for multi-file operations.
    Concurrent readers only see committed operations.
    Works with remote file shares too.
  • The TxF resource manager can participate in heterogeneous distributed transactions.
    Ex: spanning NFTS, SQL Server and MSMQ operations.

MSDN: About Transactional NTFS
MSDN Magazine article: Enhance Your Apps With File System Transactions
Wikipedia: Transactional NTFS

Sample using NTFS Transaction with SQL Server

CREATE TABLE [dbo].[DocumentLink](
	[DocumentID] [uniqueidentifier] ROWGUIDCOL primary key  NOT NULL,
	[Name] [varchar](255) UNIQUE  NOT NULL,
	[Type] [varchar](5) NULL)
Imports System.Transactions

Module Module1
    Private _testFolderPath = IO.Path.Combine(IO.Directory.GetParent(My.Application.Info.DirectoryPath).Parent.FullName, "TestFiles")

    Sub Main()
        Try
            Using tsc As New TransactionScope(TransactionScopeOption.RequiresNew,
                                              New TransactionOptions With {.IsolationLevel = IsolationLevel.ReadCommitted}),
                  DB As New FileStreamTestEntities

                'Create file in NTFS
                Dim filePath = IO.Path.Combine(_testFolderPath, "TestFile1.txt")
                Dim content = System.Text.Encoding.UTF8.GetBytes("My test data")
                Dim fileHandle = TxF.File.CreateAndWriteFile(filePath, TxF.File.CreationDisposition.CreatesNewfileAlways, content)

                'Insert link to file in database table
                DB.DocumentLinks.Add(New DocumentLink With {.DocumentID = Guid.NewGuid, .Name = filePath})
                DB.SaveChanges()

                tsc.Complete()
            End Using
            Console.WriteLine("done")
        Catch ex As Exception
            Console.WriteLine(ex.ToString)
        Finally
            Console.ReadLine()
        End Try
    End Sub

End Module

About Peter Meinl

IT Consultant
This entry was posted in Computers and Internet and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s