The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

ed@staffin.com (9) [Avatar] Offline
#1
Hi, I have been studying pages 264 and 265 for a couple of days now and can't understand the error I am getting. Basically, I have a web page so everything is disconnected. When the user wants to save changes a call to Update is made and in there I use the ever simple:

Using oScope As New TransactionScope
' Call a function to get the item to update
oObjectToUpdate = LoadFromPrimaryKey(...)
...update the object and call submit changes.
oScope.Complete()
End Using

Inside the transaction scope I call a function called LoadFromPrimaryKey which, as it's name suggests, returns and object from a linq2sql query based on some value.

The issue is that when I try to make this call, I get the following error:
"Communication with the underlying transaction manager has failed."

I have tried googliing it but there's not much help there. If I comment out the transaction code the call to LoadFromPrimaryKey works fine. I've tried looking at Attach, but I'm failing before I get there.

Any ideas?
Thanks ... Ed
jwooley (123) [Avatar] Offline
#2
Re: Transaction Trouble
What is the purpose of using the transaction scope? If you are using a single context/connection and single object structure with a single update, you don't need it. A transaction is created for you by the context in that case. The transactionscope is mainly used when spanning multiple contexts on the corresponding SubmitChanges methods.

If you are trying to set-up a read transaction, that is not supported with LINQ to SQL even with the transaction scope object.

Jim
ed@staffin.com (9) [Avatar] Offline
#3
Re: Transaction Trouble
Hi Jim, thanks for hte quick response. Here's what I am trying to do. I am using FarPoint's spread control as my grid. Each row in the spreadsheet equates to a record from teh db. The user edit's the grid and hits save. At this point I walk the spreadsheet row by row, load an object using a key and updating fields in that object based on the changes in the spreadsheet. The real code looks something like what I have below. I tried to simplify it as best I could. The point is that I am looping through a datatable, getting dbml objects and updating/inserting/deleting depending on the situation. That is why I wrapped it in a transaction.
Are you suggesting I don't need to in this case?
Thanks ... Ed



Dim db As New ThurstonDataContext
dim dt as datatable = CType(ss.DataSource, DataSet).Tables(0).GetChanges

Try
Using oScope As New TransactionScope
' start at the bottom and work up because of possible deletes.
For i = dt.Rows.Count - 1 To 0 Step -1
dRow = dt.Rows(i)

Select Case dRow.RowState
Case DataRowState.Added
' sCode will be "" and sngQty will be -1 if it's the new empty row so skip it if it is.
oTest = New lnqTest
With oTest
.Test_Desc = sDesc
.Lot_Test = bLot
End With
db.lnqTests.InsertOnSubmit(oTest)
End If
Case DataRowState.Modified
If dRow("Delete"smilie = True AndAlso lKey <> -1 Then
oTest = lnqTest.LoadByPrimaryKey(db, lKey)
If oTest IsNot Nothing AndAlso oTest.IsDeletable(lKey) Then
db.lnqTests.DeleteOnSubmit(oTest)
dt.Rows(i).Delete()
Else
hidErr.Value = "Test '" & sDesc & "' cannot be deleted at this time as it is currently referenced in other transactions."
Return
End If
Else
' normal update
oTest = lnqTest.LoadByPrimaryKey(db, lKey)
If oTest IsNot Nothing Then
oTest.Test_Desc = sDesc
oTest.Lot_Test = bLot
End If
End If
End Select
Next
db.SubmitChanges()
oScope.Complete()
End Using

Catch ex As Exception
hidErr.Value = ex.Message
Error_Log.Handle_Error(ex)
End Try
jwooley (123) [Avatar] Offline
#4
Re: Transaction Trouble
Correct, you don't need a transaction. The context will create one for you in your unit of work SubmitChanges call at the end. The explicit transaction is only needed if you are spanning databases/connections/contexts in your update process.

Jim
ed@staffin.com (9) [Avatar] Offline
#5
Re: Transaction Trouble
Wow! Even easier still! Love it. Gotta love linq. BTW, I found a nasty bug in the code generator the other day. I submitted it to microsoft and they confirmed it. Is there a place here where I can post to tell people about it?
Thanks ... Ed
jwooley (123) [Avatar] Offline
#6
Re: Transaction Trouble
You can post it here in a new thread. Alternatively, you might want to post it to the following MSDN forums:

LINQ General:
http://forums.microsoft.com/Forums/ShowForum.aspx?ForumID=123&SiteID=1

LINQ to SQL:
http://forums.microsoft.com/Forums/ShowForum.aspx?ForumID=2035&SiteID=1

I might be able to add a blog post about it on http://www.ThinqLinq.com