IBM Support

How can one consume exceptions within transaction boundaries and still enable transaction rollback?

Question & Answer


Question

How can one consume exceptions within transaction boundaries and still enable transaction rollback?

Answer

Problem Description:

In many scenarios, customers do not want to throw the Sterling exceptions to their clients in the response. If any error occurs during the process, they want to handle it internally and send an appropriate document to the client. During this if, they consume the Sterling exception for the transaction rollback will not happen and there can be records created in the database.
Scenario:
Consider a scenario where creating a return for sales order throws an exception. Customer wants to send a document to client synchronously on success or on failure. For success, they will send the Return order details and for failure, they will send a meaningful message if any error occurs during return creation. The customer wants to send a useful message to the client and not the Sterling error message/stack. Hence, they catch the exception and send a custom message to the client. However, in this process, they consume the exception and the environment/calling program does not get any error. This will cause the DB to commit the changes made during the transaction. As a result, there can be inconsistency/corruption of data.
Solution:
The customized application must throw an exception thrown from the environment for the transaction to rollback. If it consumes the exception at environment level, then the parent transaction cannot rollback.
To ensure that the error/exception does not get lost - before making the API call, create a new YFSEnvironment using YIFClientFactory. Call the API within a try-catch block in the new environment.
Steps: -
       
  • Within the Try block where the Sterling Application invokes the API in the java class, put a new try-catch block.    
             
    • In the inner try block, create a new YFSEnvironment using YIFClientFactory and then call the API using invoke or execute method.
    •        
    • Catch and throw the exception if any in the inner catch block.
    •        
    • This rolls back the database updates.
    •    
       
  •    
  • In the outer catch block, return the document as required.    
             
    • This will not throw any error to the client; instead, the outer catch block will consume it.
    •    
       
  •    
  • Make sure you release the created environment.
References:
Please refer to the documentation - YIFApi (Java Docs) for creating/releasing the new environment and invoking the API.
Sample code:
public class ThrowAndConsumeException {
public YFCDocument OuterEnv (YFSEnvironment env, Document doc) throws YFSException {
try {
InDoc = YFCDocument.getDocumentFor(doc);
try {
YIFApi oApi = YIFClientFactory.getInstance().getApi();
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = fac.newDocumentBuilder();
Document environmentDoc = docBuilder.newDocument();
Element envElement = environmentDoc.createElement("YFSEnvironment");
envElement.setAttribute("userId", "ReturnOrderTest");
envElement.setAttribute("progId", "ReturnOrderTest");
environmentDoc.appendChild(envElement);
YFSEnvironment env1 = oApi.createEnvironment(environmentDoc);
outDoc = (YFCDocument) oApi.executeFlow(env1, sMethodName, InDoc.getDocument());
oApi.releaseEnvironment(env1); //this should be done in finally block
} catch (Exception e) {
throw e;
}
} catch (Exception ex){
try {
YFCDocument errorDoc = outDoc;
return errorDoc;
} catch (Exception exception) {
throw new CustomException (“Error in sending error file”);
}
}
return outDoc;
}

[{"Product":{"code":"SS6PEW","label":"IBM Sterling Order Management"},"Business Unit":{"code":"BU048","label":"IBM Software"},"Component":"Extensions","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"All","Edition":"","Line of Business":{"code":"LOB59","label":"Sustainability Software"}},{"Product":{"code":"SS6PEW","label":"IBM Sterling Order Management"},"Business Unit":{"code":"BU048","label":"IBM Software"},"Component":"Adapters","Platform":[{"code":"","label":""}],"Version":"","Edition":"","Line of Business":{"code":"LOB59","label":"Sustainability Software"}}]

Historical Number

HTG2471

Document Information

Modified date:
16 June 2018

UID

swg21561993