1

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. In 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

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
g8torPaul
  • 1,193
  • 1
  • 7
  • 12

2 Answers2

1

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

g8torPaul
  • 1,193
  • 1
  • 7
  • 12
0

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);
}
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • The boilerplate code that you just posted comes with the JCO as an example and is pretty clear that it does not answer my question. Your finally block invokes the end() method of the JCoContext, but does not catch any exceptions there. Your assertion that the end() method does not throw exceptions is false. The API doc for the end() method states: "....JCoException - in case releasing/closing the connection runs into an issue". If this occurs in the code that you posted, that exception would take precedence over any prior exception during the execute() methods above it. – g8torPaul Jan 20 '16 at 13:10
  • Yes, I admit I was wrong. Are you using eng() statement inside finally block? Maybe this exception caused by prior exception thrown in catch block, which was hidden? – Suncatcher Jan 21 '16 at 09:28
  • The end() method is called during the finally block (which was indicated in my original post and again in the code that you posted). I am specifically talking about a potential exception thrown from the end() method. Under what conditions does this method throw an Exception? If an exception was thrown, the LUW in SAP will not be completed. This would be bad. There does not appear to be any API in the JCO to resolve this. Hence my original question. I was hoping to have someone with deep understanding of the JCO/SAP code help here. If you do not have that knowledge, do not reply to this post. – g8torPaul Jan 21 '16 at 17:45