SAP OData Soft State with Delayed Invocation – for Locking?
The so-called “soft state” mode enables the SAP NetWeaver Gateway runtime to process several requests in one ABAP application server session, similar to stateful behavior.
– copied from Andre Fischer ‘s blog post 😀 ( I am too lazy, I guess )
But in the latest releases (>7.5, not sure about the exact version), you can control the soft state behavior like opening & closing a soft state whenever it is required, and this blog post is about exploring that option 🙂
Links that you should check
You can check the blog post by Andre Fischer for more information on Soft state:
[Before checking this out, if you don’t know about the soft state, check the blog by Andre, that is mentioned in the above help link]
As I already mentioned in the intro the soft state in the earlier versions doesn’t have the functionality to start and stop on demand. It used to start once the OData service entity operation is performed.
This was a big issue, as we usually need the soft state session on a particular entity or an action.
For e.g., before updating a Sales Order, we can activate the soft state and create a lock on the sales order, In the meanwhile UI can update the sales order. Once the operation is done, we will close the soft state, then all those locks or buffered data is deleted along with the session.
[Of course, Soft state has other important purposes, but in the above example I am talking particularly about the locking use case]
To try this feature, I will take a small example as detailed below:
- An OData service with only one entity, let’s call it as Carrier.
- Two Function Imports
- One for Starting the Soft State
- Other for Stoping the Soft State.
- We will use Carrier EntitySet to perform Count Operation
- This count doesn’t count the records, but just a dummy count.
- I will have a static variable, that will be incremented every time the count operation is performed.
- But The static variable will only persist the incremented value only if there the soft state is activated else(makes sense) else it will always show the value ‘1’.
Step 1: Create a basic OData service with an entity as mentioned above.
Step 2: Redefine the MPC_EXT “DEFINE” method and call the below code
In MPC_EXT, pass the mode as “Delayed”
METHOD define. super->define( ). model->set_soft_state_enabled( EXPORTING iv_soft_state_enabled = abap_true " Enable Soft-State iv_mode = /iwbep/if_mgw_core_types=>gcs_soft_state_mode-delayed " Enable delayed invocation ). ENDMETHOD.
Step 3: Create two function imports in SEGW as described in the above steps and write the below code
METHOD /iwbep/if_mgw_appl_srv_runtime~execute_action. CASE iv_action_name. WHEN 'StartSoftState'. /iwbep/if_mgw_conv_srv_runtime~soft_state_session_start( ). WHEN 'StopSoftState'. /iwbep/if_mgw_conv_srv_runtime~soft_state_session_end( ). ENDCASE. ENDMETHOD.
Here we will call the methods soft_state_session_start & end, to manipulate the soft session instance.
Step 4: And redefine the OPERATION_END & OPERATION_START which are required for the soft state.
Step 5: Now create a static variable “Count” in the DPC_EXT class of type integer.
Step 6: Increment the “Count” variable whenever the getEntitySet method is called.
METHOD carrierset_get_entityset. count = count + 1. es_response_context-count = count. ENDMETHOD.
And Activate the soft state in the **MAINT_SERVICE and provide a timeout value in the SICF service.
Note: Even if you manually start the soft state using the function import as described above, once the timeout period is completed, the soft state session will be auto-deleted.
That’s it, Now we just need to play with the requests:
Call the EntitySet with count param multiple times, but it will return only the value ‘1’.
But once you call the function import “StartSoftState” , then the soft state is opened
After that, if we call the EntitySet multiple times, the count value gets incremented.
If we want to close the soft state, call the StartSoftState function import, then the soft state will be deleted.
Thant’s it, folks, thanks for reading this blog post. Let me know your thoughts 🙂
PS: This question of starting & stopping soft state has been asked many times at SAP Community and I was surprised to see this feature provided by SAP. In fact, one of my friend’s colleague was having the same question, I said it is not possible 😀 😀 , but after some time, he came back saying that this feature is provided by SAP in the recent releases. I don’t have his contact, but thanks for sharing this man 🙂
Thanks for this Mahesh Kumar Palavalli.
I used the soft state on a recent project. When using soft state and you are locking obejcts it can become a rather hard exercise. So I used a interval timer on the frontend to send a request to my function to extend the timeout. This way you can continue to have a low timeout still maintain the lock.
samplecode for my interval
I have my timeout set to 30 seconds now.
True, that is also a good way to extend the timeout.. Thanks for the tip Jakob Marius Kjær
Hi Mahesh Kumar Palavalli ,
Thanks for sharing the information.
I have used the same process and it's working fine through WebIDE test runs, however it's giving "Soft-State not enabled" error after deploying into Fiori Launchpad.
Is there any specific configuration or step need to be applied after deployments?
Hi Mahesh Kumar Palavalli. ,
Very good blog. Thanks for sharing. I tried to implement the softstate mode for locking/ unlocking object but in the method soft_state_session_start, the exception 'soft_state_session_start' is still raising. I followed the standard execution to find a solution but without result. However i have seen that the http request has the good values and soft state is enabled when the request enters the sap system but parameters are not giving to /IWBEP/IF_MGW_CONV_SRV_RUNTIME interface.
Do you have any idea of where the issue can come ?
Also i put break point in methode operation_start and operation_end but i didn't stop into.
Thanks for your expertise
It's been a long time since I worked on this, not exactly sure what the issue might be 🙁 please post a question at https://answers.sap.com, someone with right experience might help you out!
Hello, i have found the solution. I switched Odata model to useBatch false. With this option, http header is well filled and read. Thanks for answering.