1
votes

All,

We have currently developed a Java client application that communicates with SAP via the JCO v3.0 API. During a stateful call, where multiple BAPI functions are invoked, we use the JCoContext class to establish a stateful session. We call JCoContext.begin() before the JCoFunctions are executed and JCoContext.end() in a finally block after all functions are complete.

The problem arises on JCoContext.end(). It could potentially raise a JCoException. The documentation is not very clear on why an Exception may occur or how to handle it. In testing, I have been able to observe an exception when a NULL destination is passed to the JCoContext.end() method. While this can occur, it most definitely will not occur in our production code. So the question remains, are there any other reasons why an exception would be raised?

Also, I have observed strange behavior if the JCoContext.end() method is not invoked. It those cases, I have seen subsequent BAPI calls not complete successfully. This indicates to me that if the end() method does not complete successfully, it could leave the JCo in a bad state. So, how should we handle an exception that occurs here. What recourse do I have as a developer? It appears that the only guarantee I have is to shut the program down.

Let me know if you have any ideas, thanks,

Paul Manning

2

2 Answers

1
votes

The question was answered by an SAP employee on the SCN forums. I cross-posted the issue there. Here is the link:

SAP Forum Question: SAP JCo: How to handle an Exception thrown by JCoContext.end()

As a summary, the JCoContext.end() method should only raise an Exception if the "destination" parameter that is passed in is NULL or there is a bug in the SAP JCO. So, you must ensure that the "destination" is non-null. If you encounter another issue, submit a support ticket to SAP.

Over and out.

-Paul

0
votes

Basic understanding of JCo exceptions can be achieved with this description. And SAP documentation states this style of exception handling:

JCoDestination destination = JCoDestinationManager.getDestination("<destination>");

try
{
    JCoContext.begin(destination);
    function1.execute(destination);
    function2.execute(destination);
    functionBapiTransactionCommit.execute(destination);
}
catch (AbapException ex)
{
    ...
}
catch (JCoException ex)
{
    ...
}
catch (Exception ex)
{
    ...
}
finally
{
    JCoContext.end(destination);
}