I am curious as to the best approach to take regarding when to call Adapter.StartTransaction.
There are basically 2 approaches that one could take when performing multiple CRUD operations on a set of related entities.
** Option A**
Create a parent entity and attach all related entities to it, then call Adapter.SaveEntity(), use the proper overload and the entity, all key values, and all related entities should be created. (Atleast I think thats how it works)
** Option B**
Create a parent entity, save it using Adapter.SaveEntity, refetch the key values. Pass the key values on, create the related entitties (either via collection or one at a time) then save them.
Typically, most people create transactions as late as possible and close them as soon as possible, just like connections.
When you look at the samples in the help, you typically see transactional code like this:
dim myDB as new DataAccessAdapter
myDB.StartTransaction
Try
'.....Do Some Work
myDB.Commit
Catch
myDB.RollBack
Finally
myDB.Dispose
End Try
If I code using the practices outlined in option A, could you not write your code like this:
dim myDB as DataAccessAdapter
Try
'.....Do Some Work / build some entities and set their properties
myDB = new DataAccessAdapter
myDB.StartTransaction
if myDB.SaveEntity(someEntity, False, Nothing, True) then
myDB.Commit
end if
Catch
myDB.RollBack
Finally
myDB.Dispose
End Try
Writing code using option B would pretty much require coding your transaction like the first code sample, because the methods called for related entities physically interact with the database
Is there anthing that we should know about regarding how the StartTransaction method works internally?