Transactional saving


Author
Message
Andria Jensen
Andria Jensen
StrataFrame User (426 reputation)StrataFrame User (426 reputation)StrataFrame User (426 reputation)StrataFrame User (426 reputation)StrataFrame User (426 reputation)StrataFrame User (426 reputation)StrataFrame User (426 reputation)StrataFrame User (426 reputation)StrataFrame User (426 reputation)
Group: Forum Members
Posts: 336, Visits: 497
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()


StrataFrame Team
S
StrataFrame Developer (4.5K reputation)StrataFrame Developer (4.5K reputation)StrataFrame Developer (4.5K reputation)StrataFrame Developer (4.5K reputation)StrataFrame Developer (4.5K reputation)StrataFrame Developer (4.5K reputation)StrataFrame Developer (4.5K reputation)StrataFrame Developer (4.5K reputation)StrataFrame Developer (4.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
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).
Andria Jensen
Andria Jensen
StrataFrame User (426 reputation)StrataFrame User (426 reputation)StrataFrame User (426 reputation)StrataFrame User (426 reputation)StrataFrame User (426 reputation)StrataFrame User (426 reputation)StrataFrame User (426 reputation)StrataFrame User (426 reputation)StrataFrame User (426 reputation)
Group: Forum Members
Posts: 336, Visits: 497
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?

 


Chan
Chan
Advanced StrataFrame User (715 reputation)Advanced StrataFrame User (715 reputation)Advanced StrataFrame User (715 reputation)Advanced StrataFrame User (715 reputation)Advanced StrataFrame User (715 reputation)Advanced StrataFrame User (715 reputation)Advanced StrataFrame User (715 reputation)Advanced StrataFrame User (715 reputation)Advanced StrataFrame User (715 reputation)
Group: Forum Members
Posts: 533, Visits: 2K
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
Larry Caylor
Larry Caylor
StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)
Group: Awaiting Activation
Posts: 592, Visits: 3.7K
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

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search