简体   繁体   English

使用 JCO 为连续调用 RFC 保留 SAP 的 RFC 数据

[英]Keeping SAP's RFC data for consecutive calls of RFC using JCO

I was wondering if it was possible to keep an RFC called via JCO opened in SAP memory so I can cache stuff, this is the scenario I have in mind:我想知道是否可以在 SAP 内存中打开通过 JCO 调用的 RFC,以便我可以缓存内容,这是我想到的场景:

Suppose a simple function increments a number.假设一个简单的函数增加一个数字。 The function starts with 0, so the first time I call it with import parameter 1 it should return 1.该函数从 0 开始,所以当我第一次使用导入参数 1 调用它时,它应该返回 1。

The second time I call it, it should return 2 and so on.我第二次调用它时,它应该返回 2,依此类推。

Is this possible with JCO? JCO 可以做到这一点吗?

If I have the function object and make two successive calls it always return 1.如果我有函数对象并连续调用两次,它总是返回 1。

Can I do what I'm depicting?我可以做我所描绘的吗?

Designing an application around the stability of a certain connection is almost never a good idea (unless you're building a stability monitoring software).围绕某个连接的稳定性设计应用程序几乎从来都不是一个好主意(除非您正在构建一个稳定性监控软件)。 Build your software so that it just works, no matter how often the connection is closed and re-opened and no matter how often the session is initialized and destroyed on the server side.构建您的软件,使其正常工作,无论连接关闭和重新打开的频率如何,也无论会话在服务器端初始化和销毁​​的频率如何。 You may want to persist some state using the database, or you may need to (or want to) use the shared memory mechanisms provided by the system.您可能希望使用数据库来持久化某些状态,或者您可能需要(或想要)使用系统提供的共享内存机制。 All of this is inconsequential for the RFC handling itself.所有这些对于 RFC 处理本身来说都是无关紧要的。

Note, however, that you may need to ensure that a sequence of calls happen in a single context or "business transaction".但是请注意,您可能需要确保在单个上下文或“业务事务”中发生一系列调用。 See this question and my answer for an example.这个问题和我的回答为例。 These contexts are short-lived and allow for what you probably intended to get in the first place - just be aware that you should not design your application so that it has to keep these contexts alive for minutes or hours.这些上下文是短暂的,并且允许您首先可能想要获得的内容 - 请注意,您不应该设计您的应用程序,以便它必须使这些上下文保持活动几分钟或几小时。

The answer is yes.答案是肯定的。 In order to make it work, you need to implement two tasks:为了使其工作,您需要实现两个任务:

  1. The ABAP code needs to store its variable in the ABAP session memory. ABAP 代码需要将其变量存储在 ABAP 会话内存中。 A variable in the function group's global section will do that.功能组的全局部分中的变量将执行此操作。 Or alternatively you could use the standard ABAP technique "EXPORT TO MEMORY/IMPORT FROM MEMORY".或者,您可以使用标准的 ABAP 技术“导出到内存/从内存导入”。
  2. JCo needs to keep the user session between calls. JCo 需要在调用之间保持用户会话。 By default, JCo resets the backend-side user session after every call, which of course destroys all data stored in that user session memory.默认情况下,JCo 在每次调用后重置后端用户会话,这当然会破坏存储在该用户会话内存中的所有数据。 In order to prevent it, you need to use JCoContext.begin() and JCoContext.end() to get a stateful RFC connection that keeps the user session alive on backend side.为了防止它,您需要使用 JCoContext.begin() 和 JCoContext.end() 来获取有状态的 RFC 连接,使用户会话在后端保持活动状态。

Sample code:示例代码:

JCoDestination dest = ...
JCoFunction func = ...
try{
   JCoContext.begin(dest);
   func.execute(dest); // Will return "1"
   func.execute(dest); // Will return "2"
}
catch (JCoException e){
   // Handle network problems, ABAP exceptions, SYSTEM_FAILUREs
}
finally{
   // Make sure to release the stateful connection, otherwise you have
   // a resource-leak in your program and on backend side!
   JCoContext.end(dest);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM