Integrazione con altre applicazioni > Adattatori Info*Engine > SAP Adapter Guide > SAP Webject Library > Transaction Management Example
  
Transaction Management Example
The following examples illustrate how the various transaction webjects may be used in a task or a JSP to effectively manage a transaction in SAP.
The example described below replaces an existing document D44 (associated with a material P22) with a new document D55, and then associates the new document to the material P22. This is achieved as follows:
1. Start a transaction
2. Create a new document (D55) and associate it to the material P22
3. Delete the association between the document D44 and material P22
4. Delete the document D44
5. End the transaction
When using the Execute-RFC webject in a transaction, every invocation of the webject should typically be followed by the execution of some custom Java scriptlet code that handles any error returned by the webject.
As illustrated below, if the Execute-RFC webject returns an error, then the Throw-Exception webject is executed, causing control to go to the catch or failure block, where the transaction is rolled back through the Rollback-Transaction webject.
<%
String returnType = ieService.getAttributeValue ("OUTPUT", "IE_GROUP",
"RETURN", "TYPE");
If (returnType != null && (returnType.equals ("E") || returnType.equals ("A")))
{ %>
<ie:webject name="Throw-Exception" type="MGT"/>
<% } %>
In the example, "ieService" is a variable that refers to the Info*Engine object. This variable is established as follows:
<ie:getService varName="ieService"/>
The getAttributeValue() method used in the custom code returns the value of a named attribute of an element selected by attribute name and value. The specified group is searched for an element that contains an attribute with a specified name and value (the key). If a matching element is found, the value of the specified attribute of the element is returned. This method either returns the attribute value; or returns null if the group is not found in the collection, the element is not found in the group, or the attribute is not found in the element.
* 
Use this method when you know that an attribute is single-valued. Otherwise, use getAttributeValues.
In the example, getAttributeValue() method takes the following arguments:
groupName ("OUTPUT")—the name of the group from which to get the value.
keyName ("IE_GROUP")—the name of the attribute used as a key to select an element from the group.
keyValue ("RETURN") —the value of the attribute used as a key to select an element from the group
attrName ("TYPE")—the name of the attribute from which to retrieve a value
In the example, if getAttributeValue() returns an E (meaning “Error”) or an A (meaning “Abort”), an exception is thrown.
The following are the JSP and XML tasks that demonstrate the above transaction in different ways.
1. ExecuteRFCTransaction1.jsp
This sample JSP makes use of try/catch blocks to manage the above transaction.
<%@page language="java" session="false" errorPage="IEError.jsp"%>
<%@taglib uri="http://www.ptc.com/infoengine/taglib/core" prefix="ie"%>
<html>
<head>
<title>Transaction in SAP</title>
</head>
<body bgcolor="#ABCDEF">

<ie:getService varName="ieService"/>

<% try {
String returnType = "";
%>

<ie:webject name="Start-Transaction" type="ACT">
<ie:param name="INSTANCE" data="sapAdapter"/>
<ie:param name="GROUP_OUT" data="session"/>
</ie:webject>

<ie:webject name="Execute-RFC" type="ACT">
<ie:param name="INSTANCE" data="sapAdapter"/>
<ie:param name="SESSION_ID" data="$(session[]session_id[])"/>
<ie:param name="RFC" data="BAPI_DOCUMENT_CREATE"/>
<ie:param name="FIELD" data="DOCUMENTDATA.DOCUMENTTYPE=DRW"/>
<ie:param name="FIELD" data="DOCUMENTDATA.DOCUMENTNUMBER=D55"/>
<ie:param name="FIELD" data="DOCUMENTDATA.DOCUMENTPART=000"/>
<ie:param name="FIELD" data="DOCUMENTDATA.DESCRIPTION=D55 TESTING"/>
<ie:param name="FIELD" data="DOCUMENTDATA.DOCUMENTVERSION=-"/>
<ie:param name="FIELD" data="OBJECTLINKS.OBJECTTYPE=MARC"/>
<ie:param name="FIELD" data="OBJECTLINKS.OBJECTKEY=P22 1100"/>
<ie:param name="FIELD" data="OBJECTLINKS.DOCUMENTDIRECTION=X"/>
<ie:param name="ATTRIBUTE" data="RETURN.TYPE"/>
<ie:param name="ATTRIBUTE" data="RETURN.ID"/>
<ie:param name="ATTRIBUTE" data="RETURN.NUMBER"/>
<ie:param name="ATTRIBUTE" data="RETURN.MESSAGE"/>
<ie:param name="GROUP_OUT" data="OUTPUT"/>
</ie:webject>

<ie:webject name="Display-Table" type="DSP"/>

<%
returnType = ieService.getAttributeValue ("OUTPUT", "IE_GROUP",
"RETURN", "TYPE");
if(returnType!=null && (returnType.equals("E") || returnType.equals("A")))
{ %>
<ie:webject name="Throw-Exception" type="MGT"/>
<% } %>

<ie:webject name="Execute-RFC" type="ACT">
<ie:param name="INSTANCE" data="sapAdapter"/>
<ie:param name="SESSION_ID" data="$(session[]session_id[])"/>
<ie:param name="RFC" data="BAPI_DOCUMENT_CHANGE"/>
<ie:param name="FIELD" data="DOCUMENTTYPE=DRW"/>
<ie:param name="FIELD" data="DOCUMENTNUMBER=D44"/>
<ie:param name="FIELD" data="DOCUMENTPART=000"/>
<ie:param name="FIELD" data="DOCUMENTVERSION=-"/>
<ie:param name="FIELD" data="OBJECTLINKS.DELETEVALUE=X"/>
<ie:param name="FIELD" data="OBJECTLINKS.OBJECTTYPE=MARC"/>
<ie:param name="FIELD" data="OBJECTLINKS.OBJECTKEY=P22 1100"/>
<ie:param name="FIELD" data="OBJECTLINKS.DOCUMENTDIRECTION=X"/>
<ie:param name="ATTRIBUTE" data="RETURN.TYPE"/>
<ie:param name="ATTRIBUTE" data="RETURN.ID"/>
<ie:param name="ATTRIBUTE" data="RETURN.NUMBER"/>
<ie:param name="ATTRIBUTE" data="RETURN.MESSAGE"/>
<ie:param name="GROUP_OUT" data="OUTPUT"/>
</ie:webject>

<ie:webject name="Display-Table" type="DSP"/>

<%
returnType = ieService.getAttributeValue ("OUTPUT", "IE_GROUP",
"RETURN", "TYPE");
if(returnType!=null && (returnType.equals("E") || returnType.equals("A")))
{ %>
<ie:webject name="Throw-Exception" type="MGT"/>
<% } %>

<ie:webject name="Execute-RFC" type="ACT">
<ie:param name="INSTANCE" data="sapAdapter"/>
<ie:param name="SESSION_ID" data="$(session[]session_id[])"/>
<ie:param name="RFC" data="BAPI_DOCUMENT_DELETE"/>
<ie:param name="FIELD" data="DOCUMENTTYPE=DRW"/>
<ie:param name="FIELD" data="DOCUMENTNUMBER=D44"/>
<ie:param name="FIELD" data="DOCUMENTVERSION=-"/>
<ie:param name="FIELD" data="DOCUMENTPART=000"/>
<ie:param name="ATTRIBUTE" data="RETURN.TYPE"/>
<ie:param name="ATTRIBUTE" data="RETURN.ID"/>
<ie:param name="ATTRIBUTE" data="RETURN.NUMBER"/>
<ie:param name="ATTRIBUTE" data="RETURN.MESSAGE"/>
<ie:param name="GROUP_OUT" data="OUTPUT"/>
</ie:webject>

<ie:webject name="Display-Table" type="DSP"/>

<%
returnType = ieService.getAttributeValue ("OUTPUT", "IE_GROUP",
"RETURN", "TYPE");
if(returnType!=null && (returnType.equals("E") || returnType.equals("A")))
{ %>
<ie:webject name="Throw-Exception" type="MGT"/>
<% } %>

<ie:webject name="End-Transaction" type="ACT">
<ie:param name="INSTANCE" data="sapAdapter"/>
<ie:param name="SESSION_ID" data="$(session[]session_id[])"/>
</ie:webject>

<ie:webject name="Create-Group" type="GRP">
<ie:param name="ELEMENT" data="OUTPUT=TRANSACTION WAS ENDED SUCCESSFULLY"/>
<ie:param name="DELIMITER" data=":"/>
<ie:param name="GROUP_OUT" data="success"/>
</ie:webject>

<ie:webject name="Display-Table" type="DSP"/>

<% } catch(Exception ex) { %>

<ie:webject name="Rollback-Transaction" type="ACT">
<ie:param name="INSTANCE" data="sapAdapter"/>
<ie:param name="SESSION_ID" data="$(session[]session_id[])"/>
</ie:webject>

<ie:webject name="Create-Group" type="GRP">
<ie:param name="ELEMENT" data="OUTPUT=TRANSACTION FAILED;
ROLLED BACK SUCCESSFULLY"/>
<ie:param name="DELIMITER" data=":"/>
<ie:param name="GROUP_OUT" data="failure"/>
</ie:webject>

<ie:webject name="Display-Table" type="DSP"/>

<% } %>
</body>/<html>
2. ExecuteRFCTransaction2.jsp
This sample JSP makes use of unit/success/failure blocks to manage the transaction.
* 
You should not nest scriptlets in unit/success/failure tag blocks on a JSP page. Instead, create standalone tasks that contain the scriptlets. You can execute these tasks from the JSP page by using the Info*Engine task tag.
Instead of writing custom code for error processing, a task ProcessErrorFromExecuteRFC.xml is invoked after each Execute-RFC as shown below:
<ie:task uri="infoengine/examples/SAPAdapter/ProcessErrorFromExecuteRFC.xml">
<ie:param name="GROUP_IN" data="OUTPUT"/>
</ie:task>
3. ProcessErrorFromExecuteRFC.xml
This XML task contains the custom code to process any error returned by the Execute-RFC webject and throw an exception as appropriate.
This file is available on the adapter CD.
4. ExecuteRFCTransaction1.xml
This sample XML task makes use of unit/success/failure blocks to manage the transaction. In this example, after each Execute-RFC, the custom code that processes any error returned by the Execute-RFC webject is executed.
This file is available on the adapter CD.