StrataFrame Forum

Transactional saving

http://forum.strataframe.net/Topic11990.aspx

By Andria Jensen - 10/15/2007

I have 3 business objects which I want to create records for and then save on a transaction.  So, I know how the documentation says to do this and I have the following code in place:

TransactionBegin("", IsolationLevel.ReadCommitted)
AblStmtHdr.Save(True)
AblStmtDays.Save(True)
AblStmtRate.Save(True)
TransactionCommit("")

The issue here is that it is not saving out to the database correctly.  I have the following values before the transaction is commited:

AblStmtHdr.Count=1
AblStmtDays.Count=29
AblStmtRate.Count=3

However, when I check the database after the commit on the transaction I have 3 records for AblStmtHdr, 6 records for AblStmtRate, and 29 for AblStmtDays.  It has tripled the entry for AblStmtHdr, doubled it for AblStmtRate, and done a single entry for AblStmtDays (correctly).  These entries are created in a nested loop which is the only thing I can think of that could be affected anything here.  But, when I save them outside of a transaction they save correctly.  Is there anything I could be doing in code that is causing this behavior??  This code works correctly:

AblStmtHdr.Save()
AblStmtDays.Save()
AblStmtRate.Save()

By StrataFrame Team - 10/16/2007

It's a known issue with the way that the business objects save their parent business objects.  Since a business object is still dirty until its transaction commits, the child business objects try to save their parents again.  So, as a workaround, set the ParentBusinessObject property on all of the business objects to nothing before you save them on the transaction, then set it back again after the saves (in the Finally if you have it all wrapped in a try/catch).
By Andria Jensen - 10/17/2007

Okay, I have changed my code to look like the following:

Try

  TransactionBegin("", IsolationLevel.ReadCommitted)
  AblStmtRate.ParentBusinessObject =
Nothing
  AblStmtDays.ParentBusinessObject = Nothing

  Me.Save(True)
  AblStmtDays.Save(
True)
  AblStmtRate.Save(
True)

  TransactionCommit("")

Catch ex As Exception
  TransactionRollback(
"")

Finally
 
AblStmtRate.ParentBusinessObject = Me
 
AblStmtDays.ParentBusinessObject = AblStmtRate

End Try

It is now saving out the correct number of records, but the keys are not populating correctly for the parent/child relationships.  They are still the temporary values like -1, -2.  Usually these resolve after they are saved and the foreign key values are correctly set.  This is not happening in the above code.  Can you please tell me if I'm doing something incorrect here?

 

By Chan - 10/17/2007

Hi,

Try to call your ChildBO.Save() instead of ParentBO. SF BO will check and call ParentBO.Save() by itself. With this, you don't have to do save and restore your ChildBO's ParentBusinessObject.



HTH
By Larry Caylor - 10/17/2007

I ran into this issue a while back and have therefore tried to avoid using parent child relationships when I want to save objects on a transaction. If you only have one level, a parent and child, doing something like the following works well

'-- Save objects
If Me.ChildBO.IsDirty Then
'-- Saving the child will cause the ParentBO to be saved
   Me.ChildBO.Save(True)
ElseIf Me.ParentBO.IsDirty Then
'-- Only the parent will be saved
    Me.ParentBO.Save(True)
End

If you have more complex relationships (levels) things get ugly really fast. Hopefully the SF guys will get this fixed... maybe in 1.62Whistling
  

-Larry