Skip to Content
Technical Articles
Author's profile photo Andre Fischer

How to use Soft-State support for OData services

Updates

  • 17.03.2015 added information that security session management has to be activated
  • 11.10.2019 changed layout after the automatic conversion so that screen shots are again visible

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. The only difference is the behavior of the application server after the session times out: Instead of breaking the request processing with a time-out exception the server creates a new session and processes the request as usual. For the consumer the change of the application server sessions is transparent and no data is lost on the client session.

The session that is held by the ICF framework results in session held in the backend system via RFC where the data provider class can cache data in member variables. The Data Provider Cache is highlighted in green in the following figure.

HTTP Stack - Soft State.jpg

 

The soft state mode should be used for applications built on legacy functionality in the backend, especially when the functionality includes initial loading/caching of big amounts of data for a single request. A typical example would be a price calculation.

 

By using soft state, the resources/functionality which has been loaded during the initial load can be reused for the subsequent requests of the service. Thus, the main benefit of soft state is a considerable performance optimization of an OData service.

 

 

Prerequisites

 

You have created a sample OData service using the Service Builder as described in my SCN document How to Develop a Gateway Service using Code based Implementation .

 

Important:

In order to use soft state it is mandatory that HTTP Security Session Management on AS ABAP has been activated. Security session management can be managed using transaction SICF_SESSIONS as described in the SAP Online Help:

 

Activating HTTP Security Session Management on AS ABAP – User Authentication and Single Sign-On – SAP Library

 

Even if secúrity session management is not being activated a service currently is shown as active for soft state but does not work as supposed.

 

 

Overview – Implementation steps to activate Softstate for your service

 

  1. Redefine the DEFINE method of the model provider extension call
  2. Create an instance attribute MV_IS_SOFTSTATE for the DPC_EXT class that stores  the status whether soft state is activated
  3. Redefine the method /IWBEP/IF_MGW_SOST_SRV_RUNTIME~OPERATION_START to set this variable

 

 

Implementation steps in the model provider class

Steps Result
1. Start maintaining the model provider extension class ZCL_ZGW_PRODUCT_MPC_EXT
2. Redefine the DEFINE method of the MPC_EXT class method DEFINE.
  super->define( ).
  model->set_soft_state_enabled( abap_true ).
endmethod.

 

Implementation steps in the data provider class

Steps
Start maintaining the data provider class ZCL_ZGW_PRODUCT_DPC_EXT
Click on the Attributes tab and insert a new attribute with the following Settings

Attribute
MV_IS_SOFTSTATE

Level
Instance Attribute

Visibility
Private

Typing
Type

Associated Type
ABAP_BOOL

Description
TRUE if we are running in softstate mode

 DPC Maint.JPG

Click on the Methods tab and redefine the method /IWBEP/IF_MGW_SOST_SRV_RUNTIME~OPERATION_START

and enter the following line of code

 

mv_is_softstate = abap_true.

method /IWBEP/IF_MGW_SOST_SRV_RUNTIME~OPERATION_START.
  mv_is_softstate = abap_true.
endmethod.

Now click on the Methods tab and redefine the method /IWBEP/IF_MGW_SOST_SRV_RUNTIME~OPERATION_END

without entering any code.

 

Please note:
Without activating both methods you would get an exception

Now define a second (static) attribute GV_COUNT of type I to demo the use of soft state

 

Click on the Attributes tab and insert a new attribute with the following settings:

Attribute
GV_COUNT

Level
Static Attribute

Visibility
Protected

Typing
Type

Associated Type
I

Description
Store counter

 GV_COUNT.JPG
Activate your changes

Navigate back to the Service Builder and expand the folder Service Maintenance.

Expand the folder Maintain.

Right click on the entry of your Gateway Hub system  to start the transaction /IWFND/MAINT_SERVICE with filtered for your service name

Confirm the warning of being redirected to the Hub system

You will notice that the service ZGW_PRODUCT_SRV is shown as not supporting Soft-State.

If you click on the Soft-State button (1) you will get an error message

 /wp-content/uploads/2014/10/2_not_supported_563752.png
Confirm the error message.
3_Error_soft_state.png
Click on Load Metadata and confirm the sucess message
 /wp-content/uploads/2014/10/4_load_metadata_563768.png

The service is now shown such  that it supports soft state but soft state is not yet activated.

Hint:

If the service is not shown as inactive try to clear the metadata cache in the Gateway Hub as well as in the Gatebay backend system. To do so:

  1. Start transaction /IWBEP/CACHE_CLEANUP in the backend and activate the check box Cleanup Cache for all Models.
  2. Start transcation /iwfnd/cache_cleanup on the hub and run it with the check box Cleanup Cache for all Models.
 /wp-content/uploads/2014/10/6_inactive_563769.png
Change the session time out
On the lower left part on the screen click on ICF Node –> Configure (SICF)
 /wp-content/uploads/2014/10/7_icf_node_1_563793.png
Double Click on the entry of your service in the SICF tree
 /wp-content/uploads/2014/10/8_icf_node_1_563794.png

From the menu choose Service –> Change (1) and enter a Session Timeout greater than zero, for example 10 seconds.

Save your changes (2) and navigate back to the Service
Maintenance
Screen

 /wp-content/uploads/2014/10/9_icf_node_2_563796.png
Since the service should now shows up with the Softstate-Status Inactive you can now activate the softstate status by pressing the softstate button.
Confirm the warning before activating soft state for your service and confirm the information that softstate has now been activated.
 warning softstate activation.png
Use of softstate in the data provider extension class
Open ZCL_ZGW_PRODUCT_DPC_EXT in SE24

Change the implementation of the method  PRODUCTSET_GET_ENTITYSET to enable a custom counter when $count is called for the entity set ProductSet with the coding shown on the right.

The coding first checks whether /$count is requested and has implemented a custom implementation to retrieve $count.

If soft state is not enabled the number of products will be retrieved by counting the table entries in SNWD_PD.

If soft state is enabled the code checks whether the member variable GV_COUNT is inital.

If it is initial that means if the /$count is called for the first time in this session we have to retrieve the number of entries from the database.

Only if /$count is called a second time when soft state is enabled no data will be read from the database and /$count will rather be filled with the value stored in the variable GV_COUNT which is then incremented by 1 to show you that soft state is actually working.

If the time configured as a session timeout is exceeded counting starts again from the number of products which is equal to the number of table entries in table SNWD_PD.

Activate your changes.

 

 

Test the service in the browser

 

 

Steps
Call your service via the browser.
http://<server>:<port>/sap/opu/odata/sap/ZGW_PRODUCT_SRV/ProductSet/$count
When refreshing the URL you will notice that the counter will start to count the number of calls.
 count_4.JPG
Wait for 20 seconds …

When you wait for a period longer than the one configured as a session timeout in SICF the counter starts with 1 again.

Please note:
If you have configured a small time window (for example 10 seconds) you will probably have to wait longer since there is an additional latency.
If you are too impatient and click on refresh to fast the wait time starts again from zero.

 count_1.JPG

Comparison of response times

The behaviour when using soft state or not can nicely be checked using the performance trace. This can be started using transaction /IWFND/TRACES.

 

Steps

Start a performance trace using /IWFND/TRACES.

Perform several requests with $count having soft state enabled

We can see that the initial request takes 16 milliseconds in the backend (GW framework overhead + Application Data Provider response time).

For every subsequent request  the Application Data Provider Response time is zero and the GW framework overhead is reduced to 5 mill seconds.

 Count with softstate.JPG
Deactivate soft state and perform again several requests with $count

The result now is that subsequent requests have the same response time.

The response time is the same that we got for the first initial request when soft state was enabled.

Count without softstate.JPG

 

Assigned Tags

      43 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Vijay Vegesana
      Vijay Vegesana

      Thanks Andre for detailed explanation

      Author's profile photo Frank Eichholz
      Frank Eichholz

      Hi Andre, many thanks for the detailed explanation.

      Nevertheless i have two questions:

      1. Why do you added the variable MV_IS_SOFTSTATE? For the example it is not really necessary or am i wrong?
      2. The static attribute variable is different for every session (and every instance of the dpc class)?

      Best Regards, Frank

      Author's profile photo Andre Fischer
      Andre Fischer
      Blog Post Author

      Hi Frank,

      Thanks for pointing this out.

      I have thus changed the coding to really make use of my variable MV_IS_SOFTSTATE. 😉

      I have changed the coding and used the variable MV_IS_SOFTSTATE to check whether soft state is enabled or not. If it is not enabled the /$count is retrieved from the database whereas the implementation checks whether this value has already been retrieved from the backend when soft state is enabled.

      When soft state is enabled the implementation only reads the number of products from the database in the first request.

      You are right, the static attribute variable is different in every session.that means if I call the service a second time having waited longer than the time span configured as the session time out in the ICF node.

      Author's profile photo Former Member
      Former Member

      Hi

      Thanks for the detailed explanation. I have an issue where the soft state is shown as Active, but when i debug I see that the variable mv_is_softstate is not set. What am I missing here? I redefined the methods you suggested and did all other steps in the same way.

      Author's profile photo Louis-Arnaud BOUQUIN
      Louis-Arnaud BOUQUIN

      Hello,

      I have the same issue. Did you manage to solve it ?

      I can see that the method OPERATION_START is not called.

      Author's profile photo Former Member
      Former Member

      Hi Andre,

      Thanks for the detailed explanation.

      I have the following question : if we want to retrieve for instance list of countries , do we have to create a static instance for that ?

      can we usethe soft state if we qeuery for the detail of an entity (for instance detail of a product with key fields ).


      Thanks

      Author's profile photo Andre Fischer
      Andre Fischer
      Blog Post Author

      Hi Moo,

      the example of a list of countries is not the one where I would see soft state. This list should not be so expensive to retrieve so using soft state for this would not be appropriate.

      An example where soft state would be beneficial would for example be a list of products with prices where the price is expensive to calculate.

      If you want to store this you would not use an instance attribute but rather an Static Attribute because otherwise the content would be lost.

      If you just want to cache the content of a list the Softstate based query result cache might be something you want to look at. For example if you are looking for read only access. This is technically based on the soft state as well and allows to cache the result of a query in the Gateway Hub rather than in the Gateway backend.

      The details of a product are a perfect example to be stored in a static attribute of your data provider extension class if it is very expensive to retrieve those details. Example for this might be the availability or the price.

      Best Regards,

      Andre

      Author's profile photo Arshad Ansary
      Arshad Ansary

      Hi Andre,

      Thanks for an excellent blog on how to achieve stateful behaviour for GW service but I have some doubts

      I created a service with session time out in SICF set as 30 secs and enabled softstate for the service as well as in MPC define method.

      But when i fire multiple request from GW client in a span of 5 secs also, the static attribute  variable GV_COUNT is initial .It always return the count from the DB.

      I also dont understand the need of MV_IS_SOFTSTATE which will always be true irrepective of whatever services you call inside that project . That leads me to the next question of how can we enable softstate for a particular entity types(inside a projects) query operation

      Regards

      Arshad

      Author's profile photo Former Member
      Former Member

      Hello Andre,

      Thanks for your excellent blog. I have a question to soft state respectively to session handling. When the user closes their browser, how can you intercept the relationship as setting the timeout to zero? Is that anyway possible?


      Best Regards,

      Claudia

      Author's profile photo Andre Fischer
      Andre Fischer
      Blog Post Author

      Hi Claudia,

      good to hear that you like the document. You can rate it as well 😉 .

      It is not clear to me what you are looking for exactly.

      Are you looking for an option to have access to the session after a user has closed the browser?

      Or are you looking for information how the session is closed after the user has closed the browser window?

      Best Regards,

      Andre

      Author's profile photo Former Member
      Former Member

      Hi Andre,

      thank you for your reply. I'm looking for a way to close the session after the user has closed their browser window.


      Best Regards,

      Claudia

      Author's profile photo Former Member
      Former Member

      Hello Claudia,

      I have the very same requirement. I was wondering if you found a way to achieve that?

      Look forward to hear from you.

      Best Regards,

      Jay

      Author's profile photo Dmitrii Sharshatkin
      Dmitrii Sharshatkin

      Hello Jayesh,

      Please see my reply to Claudia.

      BR, Dima

      Author's profile photo Dmitrii Sharshatkin
      Dmitrii Sharshatkin

      Hello Claudia,

      You can try the following:

      1. You'll get your own JavaScript event on leaving the window, e.g. window.onbeforeunload or window.onunload.

      2. From there you'll trigger the request informing you want to close the session. E.g. /sap/opu/odata/sap/zmy_test/MyCounter/?close_session_now=X or any other means, e.g. including something into request header.

      3.  You can catch it via your own handler in SICF node or somehow in your OData DPC class. It's up to you and up to method you chose in p.2).

      4. Get the right session via: cl_server_info=> get_session_list.

      5. Kill the session via FM: TH_DELETE_USER having set all parameters: USER, CLIENT, LOGON_HDL and LOGON_ID.

      You can simulate it in transaction SM04.

      It should work, if not, let me know.

      BR, Dima

      Author's profile photo Dmitrii Sharshatkin
      Dmitrii Sharshatkin

      Small addition, in my system the function TH_DELETE_USER does not work, iа you try to kill the current session you are in. So you should kill this session from another one. The easiest way would be to use another generic OData service e.g. killing any Soft Sate OData connection by it's name or just a web-rfc function. I used the last option and it works. For web-rfc look here: WebRFC - simply calling an RFC from javascript

      BTW, I have posted a more detailed blog-post here: Closing session of Soft State OData service

      BR, Dima

      Author's profile photo Vikas Lamba
      Vikas Lamba

      Hi Andre,

      Is there a way for us to capture the event (Session Ended) in the DPC class?

      I was wondering if that can be done then I can potentially persist the state of my transient data and then reload it whenever a new session is created..

      Maybe I did not understand the role of OPERATION-END api if that is intended to this.

      Regards,

      Vikas

      Author's profile photo Andre Fischer
      Andre Fischer
      Blog Post Author

      Hi Vikas,

      the method /IWBEP/IF_MGW_SOST_SRV_RUNTIME~OPERATION_END is exactly the place where you can perform actions such as to persist the state of your transient data.

      You will not be able to catch the event session ended yourself in the DPC class because the session will be automatically terminated after the end of the session timeout is reached. At this time your user will not use the session anymore.

      Best Regards,

      Andre

      Author's profile photo Prabaharan Asokan
      Prabaharan Asokan

      Hi Andre,

      Can you shed some lights here ?

      "Nice and helpful feature in SAP Gateway. Could you please give some more insights in using "Softstate based query result cache" ? What classes and interfaces are required to required to use this feature and in which DPC method should the implemenation be done. Thanks for the help."

      Regards

      Prabaharan Asokan

      Author's profile photo Prabaharan Asokan
      Prabaharan Asokan

      Hi Andre,

      Soft state will only improve the performace  post completing the first request. But still first run will not have any performance impact when using soft state.

      Please suggest.

      Regards

      Prabaharan Asokan

      Author's profile photo Andre Fischer
      Andre Fischer
      Blog Post Author

      Hi Prabaharan,

      I am not sure whether I understood your question.

      Soft state is a feature that should not make the first request faster but any sub sequent requests.

      Think about an app that needs to determine a price for a product. Pricing is usually a very expensive calculation from a performance perspective.

      The first calculation of the price is not faster when using soft state.

      But if the app needs the price for the same product for a subsequent request your service implementation in the data provider class could take the price it has calculated a few seconds or minutes earlier that was cached in your DPC.

      The second call will then be faster.

      Best Regards,

      Andre

      Author's profile photo Brian Zhu
      Brian Zhu

      hi Andre,

      i have the same issue as Vishwa.

      i still get no luck with softstate status "Not supported" after i redefined the methonds in

      MPC and DPC extention classes.

      Did i miss something right there?

      Author's profile photo Prabaharan Asokan
      Prabaharan Asokan

      Hi Andre,

      Nice and helpful feature in SAP Gateway. Could you please give some more insights in using "Softstate based query result cache" ? What classes and interfaces are required to required to use this feature and in which DPC method should the implemenation be done. Thanks for the help.

      Regards

      Prabaharan Asokan

      Author's profile photo Kaan Ozdogan
      Kaan Ozdogan

      I create and configurate soft state but create back-end session every request. So I lost data. I did it Netweaver GW 7.01 but upgrade NW 740 I couldn't do it.

      For example:

      Sessions from SM04:

      R=2 P=/sap/opu/odata/sap/ZMOBILEDI_SRV/mobiledi_temp_recover

      P=/sap/opu/odata/SAP/ZMOBILEDI_SRV/mobiledi_overview

      Author's profile photo Former Member
      Former Member

      Hallo Andre Fischer,

       

      Zuerst einmal vielen Dank für deine How To´s. Die haben mir bei meiner bisherigen Bachelorarbeit echt weitergeholfen.

      Ich bin gerade dabei, dass serverseitige Cachen auszuprobieren um dies als Möglichkeit in meiner Arbeit zu erwähnen. Klar kann ich die Theorie dahinter aufschreiben, jedoch wollte ich es auch selber testen.

      Bisher bin ich deine Anleitung Schritt für Schritt durchgegangen, kommen jedoch zu dem Punkt, dass die Variable GV_COUNT bei jedem Aufruf initial ist. In der MPC_EXT habe ich die Define Methode redefiniert und das Model für das Softstate aktiviert. Auch die Variable GV_COUNT habe ich natürlich auf Statisch gesetzt, jedoch verhält sich das Mistding wie eine Instanzvariable ?

      Kann es vielleicht an der Sicherheitseinstellung des Service liegen?
      Die Einstellungen sind wie folgt:

      login/create_sso2_ticket = 2
      login/accept_sso2_ticket = 1
      login/ticketcache_entries_max = 1000
      login/ticketcache_off = 0
      login/ticket_only_by_https = 0
      icf/set_HTTPonly_flag_on_cookies = 3
      icf/user_recheck = 0
      http/security_session_timeout = 1800
      http/security_context_cache_size = 2500
      rdisp/plugin_auto_logout = 1800
      rdisp/autothtime = 60

      Gibt es hierbei noch irgendetwas zu beachten? Würde mich über eine Antwort sehr freuen ?

      PS: Das SAP Gatewaybuch ließt sich sehr gut ?

      Author's profile photo Andre Fischer
      Andre Fischer
      Blog Post Author

      Hi Erich,

      I am glad you like my How Tos, the book and that both helped you with your bachelor thesis.

      For further questions I would like to ask them in the fourm.

      Have you checked whether the soft state is really active ? (transaction /IWFND/MAINT_SERVICE), is the session time out configured to be > 0 s. You can check SM05 whether your security session is reused or not).

      Best Regards,

      Andre

       

      Author's profile photo Former Member
      Former Member

      Hi Andre,

      Thanks for the this very helpful post. I have a query here. Method OPERATION_START is not getting called and because of this variable mv_is_softstate is not set. Please help here.

      soft state is active and session time out configured is > 0 sec 

      SM05 entry :

       

       

       

      Best Regards,

       

      Shagun

       

      Author's profile photo Former Member
      Former Member

      Hey,

      I have implemented everything as you said. When I call the method get_entityset for the first time it gives me the number of records in the table but when i call it the second time it does not give me the call counts instead keeps incrementing the value of number of records.please help me solve this.

      regards,

      Rohan

      Author's profile photo Lahcen Babayi
      Lahcen Babayi

      Hi Andre,

      Thank you for this great blog.

      I have implemented Soft state in my Odata service.

      I have an issue where the soft state is shown as Active, but the debugger does not stop on my external breakpoints in :

      /IWBEP/IF_MGW_SOST_SRV_RUNTIME~OPERATION_START
      /IWBEP/IF_MGW_SOST_SRV_RUNTIME~OPERATION_END

      Thank you

      Lahcen

      Author's profile photo Lahcen Babayi
      Lahcen Babayi

      Hi Andre,

      Here is one more detail

      If I implement the service Directly in Gateway, the debugger works perfect.

      When I implement it in backend and publish it in Gateway, I get the issue mentioned above.

      Thank you

      Lahcen

      Author's profile photo Andre Fischer
      Andre Fischer
      Blog Post Author

      Can you please post this as a question in the Q&A section of SCN?

      Author's profile photo Lahcen Babayi
      Lahcen Babayi

       

      That is done.

      Thanks a lot

       

      Author's profile photo Yu-Syuan Lin
      Yu-Syuan Lin

      Dear Andre Fischer,

      I follow your document steps but i still get zero from browser.

      Author's profile photo Yu-Syuan Lin
      Yu-Syuan Lin

      My service Soft-state enable in gateway hub.

      Set session time out greater than zero from T-code SICF.

      Implementation is in DPC_EXT

      Implementation is in MPC_EXT.

      Author's profile photo Sandra Rossi
      Sandra Rossi

      Simply because your table SNWD_PD is empty ! Why not using another table (not empty but small) for the test ?

      Be careful, you entered a time out of 10 MINUTES (instead of 10 seconds), do you really want to wait more than 10 minutes between each test ?

      Author's profile photo Yu-Syuan Lin
      Yu-Syuan Lin

       

      Could you help to check my practice screenshot?

      thanks.

      Author's profile photo Andre Fischer
      Andre Fischer
      Blog Post Author

      have you tested the scenario using the SAP Gateway client ?

      This should reuse the session unless you have to specify it in older releses.

       

      Author's profile photo Yu-Syuan Lin
      Yu-Syuan Lin

      I have test in gateway client and still can not get soft-state effects.

      what is older releases that you mentioned?

      thanks.

      Author's profile photo Andre Fischer
      Andre Fischer
      Blog Post Author

      In older Releases there is a check box you have to activate in order to reuse the http session to see the effect of soft state (here 740)

       

      In newer releases (latetest SP of 750) the button is gone and the session is reused automatically.

      Regards.

      Andre

      Author's profile photo Yu-Syuan Lin
      Yu-Syuan Lin

      Hi Andre,

      This was my retest result with the check box you mentioned and still can not got soft-state effects....

      thanks.

      Author's profile photo Alban Leong
      Alban Leong

      I know that this blog is from 2014 and we’re now in 2020 – but just dropping in to say a HUGE THANKS because I was able to implement the soft state functionality and apply locking to my project with the information provided here since we’re still on a much older ABAP release and do not have any of the new fancy functionality the newer releases has – but was able to get this basic soft state functionality to work! Thanks again!

      Author's profile photo Andre Fischer
      Andre Fischer
      Blog Post Author

      You are welcome :-).

      Glad to hear that it works and though soft state has not been built to achieve locking I know that some customers are "misusing" it.

      You only have to be sure what you are doing (that means what happens if the soft state session is terminated, but the lock resides) and you have to be sure that your locking works fine.

      Having said that.

      Good luck and hope to see you back on a new release soon ;-).

      Author's profile photo Alban Leong
      Alban Leong

      Oh yes – ideally, if we were on any “newer” version like 751, would have most likely use BOPF with draft functionality… but… life’s not always ideal. LOL… and I'm certainly not trying to ignore the very fact that I could be "misusing" it. 🙂

      And yup, will certainly be testing to make sure that sessions are being released automatically as expected and db locks are being handled along with it. Thanks for the advice! We have considered other points before enabling soft state for this application such as it will be a low traffic but critical app - only about 5 users daily, making sure locks are on single record instead of locking entire table, and keeping session timeout to a minimum, etc...

      I too look forward to our move to newer or the latest release! Hopefully soon!

      Author's profile photo Shivam Srivastava
      Shivam Srivastava

      Hi Andre,

      I am trying to implement soft state functionality in OdataV4 but couldn't find the any examples. can you help/guide me to implement soft state functionality in V4 services.