MBO Performance Tip N.4 – Be careful when calling MboSet.save()

This entry is part of the Java MBO performance optimization golden rules series.

This tip regards the correct handling of transactions in TPAE.

The important concept to understand is that MBOs obtained via relationship are included in the same transaction as the parent MBO set. When any MboSet in the transaction is saved, all MboSets in that same transaction will be saved. Any redundant call to MboSet.save() method will affect performance and break the transaction chain.
Theoretically the MboSet.save() must be called once after all the needed changes have been made to optimize performances and to correclty handle database transactions.

In the following example there is an unnecessary invocation of the save() method.

MboSetRemote pos = MXServer.getMXServer().getMboSet("PO", userInfo);
MboRemote po = pos.moveFirst();
po.setValue("VALUE1", newValue1);
pos.save(); // this is a redundant save
MboSetRemote polines = po.getMboSet("POLINE");
for(MboRemote poline=polines.moveFirst(); poline!=null; poline=polines.moveNext())
{
poline.setValue(...);
}
polines.save();

The redundant save() call can be removed without problem invoking only the changes.save() method at the end.

MboSetRemote pos = MXServer.getMXServer().getMboSet("PO", userInfo);
MboRemote po = pos.moveFirst();
po.setValue("VALUE1", newValue1);
MboSetRemote polines = po.getMboSet("POLINE");
for(MboRemote poline=polines.moveFirst(); poline!=null; poline=polines.moveNext())
{
poline.setValue(...);
}
pos.save(); // this saves all the updates

Since the polines MboSet is retrieved from the pos MboSet, the pos.save() invocation will also commit all the updates on the child objects retrieved through a relationship.

Sometimes it may be a good idea to avoid keeping transactions open for long periods of time. To achieve this you can fetch MBOs using a new transaction using MXServer.getMboSet() method.

MboSetRemote mboSet = MXServer.getMXServer().getMboSet(setname, getUserInfo());

Caution! Keep in mind that transactions will be kept open longer if you delay calling save. You need to decide whether this tradeoff is worth the performance benefit in your code.

MBO Performance Tip N.4 – Be careful when calling MboSet.save()

One thought on “MBO Performance Tip N.4 – Be careful when calling MboSet.save()

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top