TransactionScope

Posts   
 
    
Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 06-Jul-2010 13:55:46   

Hello,

I tried reading as many threads as I can before posting a new one. I have the following scenario when saving a new invoice:

  • Get a new invoice number from [Counter] table.
  • Update the new invoice, modifying the number to show the new number.
  • Save the invoice
  • Update the average cost for each itemHowever, I want all of this to occur in a transaction so that if ANYTHING fails, all fails.

Here is the code I'm using now, where NOT everything is in the transaction (currently UnitOfWork):

Private Function DoSave(ByVal DisplayMessage As Boolean) As Boolean

        Try
            UpdateFocusedControl()

            'Update the metadata
            Dim x As CustomerSaleEntity = CType(Me.bindSource.Current, CustomerSaleEntity)

            'Here, I'm getting the new number
            If x.IsNew Then
                Dim newNumber As String = GetNewNumber.GetNewNumber("CustomerSale")
                x.StrCustomerSaleId = newNumber
            End If

            With x
                .StrWarehouseId = Helpers.MainWarehouseId
                .SintRecVer += 1S
                .DtmDateEntered = Date.Now
                .StrTerminalName = My.Computer.Name
                .StrUserName = My.User.Name
                .StrVer = My.Application.Info.Version.ToString
            End With

            'Display message when NOT saved
            Dim uow As New UnitOfWork
            uow.AddCollectionForDelete(Me.DeletedChildren)
            uow.AddCollectionForSave(x.CustomerSaleDetail, True)
            uow.AddForSave(Me.CurrentEntity, True)

            If uow.Commit(New Transaction(IsolationLevel.ReadCommitted, "trans"), True) = 0 Then
                If DisplayMessage = True Then
                    MessageBox.Show("Entry has NOT been saved")
                End If
                Return False
            Else
                Return True
            End If

        Catch ex As Exception
            Utils.HandleException(ex, Me)

        End Try

    End Function

What happens now is that if the invoice fails to save, the new number is still saved and I end up with a missing invoice number.

Now, I'm thinking of using TransactionScope, but is this the right approach?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 06-Jul-2010 20:38:53   

You can use either native ADO.NET transactions or TransactionScope to accomplish this - have a look at the documentation at http://www.llblgen.com/documentation/2.6/Using%20the%20generated%20code/SelfServicing/gencode_transactions.htm

Another alternative would be to wrap it all up in one stored procedure and call that, but it's not really in the LLBLGen spirit of things...simple_smile

Matt