
COM+ question: SetAbort not rolling back transaction?
I'm trying to get the hang of transactions with COM+ and VB, and I'm having
some trouble with rolling things back when they fail.
I've got a VB component that calls two subcomponents, each of which tries to
insert a record into the same table, using the same primary key value. The
first insert statement works fine, and naturally the second one fails.
However, when I try to rollback this transaction in the main component, the
first insert remains in the table-- not what I wanted.
Here's the main component's function, the one which creates and runs the two
subcomponents:
'-------------------------------------------------------------
Public Function superComponentDoSomething() as Boolean
Dim objContext as COMSVCSLib.ObjectContext
Dim testComponent, liveComponent as subComponent
Dim bResult, bResult2 as Boolean
Set objContext = GetObjectContext()
<....>
bResult = testComponent.insertTitle("hello",31) 'This works fine
bResult2 = liveComponent.insertTitle("goodbye",31) ' This call doesn't
if (bResult =false or bResult2 = false) then
objContext.SetAbort
superComponentDoSomething = false
else
objContext.SetComplete
superComponentDoSomething = true
end if
End Function
'-----------------------------------------------
As I mentioned, the subcomponents try to insert records into a SQL table,
like so:
'-----------------------------------------------------------------------
Public Function insertTitle(ByVal title as String, ByVal myID as Integer)
On error goto err_handle
Dim objContext as COMSVCSLib.ObjectContext
Set objContext = GetObjectContext()
<...>
strSQL = "Insert into myTable (uniqueValue,myTitle) values (" & myID &
",'" & title &"')"
Connection.Execute(strSQL )
objContext.SetComplete
insertTitle=true
Exit Function
err_handle:
objContext.SetAbort
insertTitle = false
End Function
'-----------------------------------------------
The first time I call this "insertTitle" function, it works fine, does the
insert. Of course, when I do it again, trying to use the same primary key,
it blows up, and the SetAbort method is called, both in the subComponent,
*and* in the main component's method.
What I thought would happen is that both insert statements would have been
aborted by the main component's SetAbort call, but that's not happening for
whatever reason.
If it helps, the main component's transaction mode is set to "Requires New
Transaction", while the subComponents are set to "Requires Transaction". If
you can see what I'm doing wrong above, or what assumptions I'm making, I'd
be grateful the hear about it. Thanks!