sql transactionscope and ntfs vb.net

I was working on a project where I had to upload a file, process this file...do some db work and work on file again. I wanted to apply 
a transaction to the entire process, if one fails everything fails (ACID process).

I ended up using .net  transactionscope class to achieve this. Below is Sample Code.

First add a reference to System.transcations Namespace

Public Sub TranMethod()

        Try

            Dim path_week_1 As String = Path.Combine(Server.MapPath("/tempdata/"), "import_updated_1.csv")
            Dim path_week_2 As String = Path.Combine(Server.MapPath("/tempdata/"), "import_updated_2.csv")

            Dim [option] = New TransactionOptions()
            [option].IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted
            [option].Timeout = TimeSpan.FromMinutes(2)

            Using scope = New TransactionScope(TransactionScopeOption.Required, [option])

                Dim upld_files As cls_FileUpload = New cls_FileUpload(path_week_1, fup_aides, path_week_2, fup_aides2)

                DAL.ExecuteQuery("EXEc [sp_Import_Aids] '" & path_week_1 & "', '" & path_week_2 & "'")

                scope.Complete()
                lbl_msg.Text = "File(s) Uploaded Successfully."

            End Using
        Catch ex As Exception
            lbl_msg.Text = ex.Message
        End Try
End Sub


The Class which handles the IO Process..



Imports Microsoft.VisualBasic
Imports System.Transactions
Imports System.IO


Public Class cls_FileUpload
    Implements IEnlistmentNotification


    Private Property _getFilePath_first As String = ""
    Private Property _getFilePath_two As String = ""


    Public Sub New(ByVal file_path_one As String, ByVal File_control_one As FileUpload, _
                   ByVal file_path_two As String, ByVal File_control_two As FileUpload
                   )

        Dim rdFile As String

        ' first File
        If File_control_one.HasFile Then

            File_control_one.SaveAs(file_path_one)

            rdFile = File.ReadAllText(file_path_one)
            rdFile = rdFile.Replace("""", "")
            File.WriteAllText(file_path_one, rdFile)

            _getFilePath_first = file_path_one
            ' End First File
        End If
        ' End First File


        ' 2nd File
        If File_control_two.HasFile Then

            File_control_two.SaveAs(file_path_two)

            rdFile = File.ReadAllText(file_path_two)
            rdFile = rdFile.Replace("""", "")
            File.WriteAllText(file_path_two, rdFile)

            _getFilePath_two = file_path_two
            ' End First File
        End If
        ' End 2nd File



        Transaction.Current.EnlistVolatile(Me, EnlistmentOptions.None)

    End Sub


    Public Sub Commit(enlistment As Enlistment) Implements IEnlistmentNotification.Commit
        ' Write Logic here like if something was set to TRUE within constructor.
        enlistment.Done()
    End Sub

    Public Sub InDoubt(enlistment As Enlistment) Implements IEnlistmentNotification.InDoubt
        enlistment.Done()
    End Sub

    Public Sub Prepare(preparingEnlistment As PreparingEnlistment) Implements IEnlistmentNotification.Prepare
        preparingEnlistment.Prepared()
    End Sub

    Public Sub Rollback(enlistment As Enlistment) Implements IEnlistmentNotification.Rollback

        If Len(_getFilePath_first) > 0 Then
            File.Delete(_getFilePath_first)
        End If
        If Len(_getFilePath_two) > 0 Then
            File.Delete(_getFilePath_two)
        End If

        enlistment.Done()

    End Sub
End Class


Here is a Good read about TranscationScope.

Posted by:

No comments posted.

Name :  

Email :  

Comment Below