Scenario 1
This scenario uses an explicit auto commit transaction that updates the Inventory table and commits and releases the table immediately before continuing with the remainder of the sales order processing. Because inventory records are committed before the sales order is committed, an error could occur during the continued processing of the sales order. If an error occurs, another business function (referred to as a compensating business function) must be called to undo the inventory updates.
You use another explicit transaction to call the compensating business function. You can either reuse the original auto commit connection or create a new connection. The best option is to reuse the original auto commit connection, because this limits the number of objects that are created. You cannot use the default transaction because you want to send an exception message to the caller indicating that the sales order processing failed, and you want to roll back any updates that were made to the inventory records. You use an explicit connection so that you can control the compensating business function to ensure that updates are rolled back, even if an exception is thrown.
This code sample illustrates this scenario:
public E1MessageList processSalesOrder(IContext context, IConnection connection, InternalProcessSalesOrder internalVO){ ... //Create new explicit auto commit connection to add inventory //records IConnection invConnection = context.getNewConnection (IConnection.AUTO); //call method (created by the wizard), which then executes Business Function or Database operation E1MessageList invMessages = callInventoryMBF(context, invConnection, internalVO, programId); //add messages returned from E1 processing to business //service message list. messages.addMessages(invMessages); if (!invMessages.hasErrors()) { //No errors continue processing SO IConnection soConnection = context.getNewConnection (IConnection.MANUAL); try { //Call SO E1MessageList soMessages = callSOMBF(context, soConnection, internalVO); //Check for errors, collect in messages. if (!soMessages.hasErrors()) { soConnection.commit(); }else{ soConnection.rollback(); //Errors in SO processing, call MBF to compensate for //added inventory E1MessageList compMessages = callInventory CompensateMBF(context,invConnection,internalVO); if(compMessages.hasErrors()){ compMessages.setMessagePrefix("Unable to Compensate for Added Inventory"); } messages.addMessages(compMessages); } } catch (BSSVConnectionException e) { //Create new error and return E1MessageList E1Message txMessage = new E1Message (context, "006FIS", e.getMessage()); messages.addMessage(txMessage); } soConnection.close(); } invConnection.close(); finishInternalMethod(context, "addAddressBook"); return messages; }