Skip to Content
Technical Articles
Author's profile photo Mahesh Palavalli

SAP OData Soft State with Delayed Invocation – for Locking?

 

Introduction:

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:

https://blogs.sap.com/2014/10/14/how-to-use-soft-state-support-for-odata-services/

Help link:

https://help.sap.com/viewer/68bf513362174d54b58cddec28794093/7.52.5/en-US/25a27ab5fb614b538bc44228fcc9b027.html

 

Exploration

[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:

  1. An OData service with only one entity, let’s call it as Carrier.
  2. Two Function Imports
    1. One for Starting the Soft State
    2. Other for Stoping the Soft State.
  3. We will use Carrier EntitySet to perform Count Operation
    1. This count doesn’t count the records, but just a dummy count.
    2. I will have a static variable, that will be incremented every time the count operation is performed.
    3. 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’.

/sap/opu/odata/sap/ZDEMOSS_SRV/CarrierSet/$count

 

But once you call the function import “StartSoftState” , then the soft state is opened

sap/opu/odata/sap/ZDEMOSS_SRV/StartSoftState

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 🙂

 

Cheers!!

Mahesh

 

Assigned Tags

      8 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Jakob Marius Kjær
      Jakob Marius Kjær

      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

      setInterval(function(){ alert("Hello"); }, 3000);

      I have my timeout set to 30 seconds now.

      Author's profile photo Mahesh Palavalli
      Mahesh Palavalli
      Blog Post Author

      True, that is also a good way to extend the timeout.. Thanks for the tip  Jakob Marius Kjær

      Author's profile photo Arnab Datta
      Arnab Datta

      Hi Jakob,

      Will the SICF timeout work in this delayed soft state enablement? Please clarify, I tried but it is not releasing the lock after timeout time set in SICF.

      Author's profile photo Praveer Kumar Sen
      Praveer Kumar Sen

      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?

      Regards,

      Praveer.

      Author's profile photo Florent Papaix
      Florent Papaix

      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

      Florent Papaix

      Author's profile photo Mahesh Palavalli
      Mahesh Palavalli
      Blog Post Author

      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!

      Author's profile photo Florent Papaix
      Florent Papaix

      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.

      Florent Papaix

      Author's profile photo Arnab Datta
      Arnab Datta

      Hi Mahesh,

      Will the SICF timeout will work if we dont unlock? Please clarify that what can be done in that case. I checked it is not releasing the lock as expected if we dont manually unlock it. Please help with this.