cancel
Showing results for 
Search instead for 
Did you mean: 

how can i lock objects with an oData Service

Benedikt3
Participant
0 Kudos

Hi,

Example:

Somebody should change an order on his mobile device. How can i lock them for other users?

(best case, linked to the session time btw. How can i show all valid sessions?)

SAP ERP an Gateway based on the latest releases; Custom Development

Problem:

The standard lock concept of SAP is based on a LUW ...  I can create a lock at the get_entity method, but this look is auto-released, after processing the request. Or in other words, the look is not bounded on the session time.....

Question:
How can I create a lock between a read and an update request,?

I found some documentation about the sross-system lock, but I could not find any ABAP code example.

Thanks and Regards

Beny

Accepted Solutions (1)

Accepted Solutions (1)

former_member182048
Active Contributor

Hi Benny

I have been meaning to write a blog on Concurrency and Caching for a while,  thanks for reminding me, for now i'll try and answer as best i can, keeping it simple, this topic can get complicated quickly.

Managing concurrency is an undocumented feature of SAP Gateway. Gateway OData feeds leverage the same mechanisms as HTTP, entity tags (ETags) and headers for detecting conflicting entity changes .

A simple explanation of the differences between optimistic and pessimistic concurrency

http://blogs.msdn.com/b/marcelolr/archive/2010/07/16/optimistic-and-pessimistic-concurrency-a-simple...

In pessimistic concurrency, we ask the database to lock the row, think traditional ABAP LUW transaction locking, in optimistic concurrency the client is responsible for holding temporary data, rather than locking the row to stop conflicts, we store and use a version reference.

"OData uses HTTP ETags for optimistic concurrency control"

http://www.odata.org/developers/protocols/operations#ConcurrencycontrolandETags

SAP Gateway Help: Supported OData Features

http://help.sap.com/saphelp_gateway20sp05/helpdata/en/d8/5d033b8edf439f9897c4e546363d78/content.htm

The Gateway service can be developed so that each individual entity provides an ETag.

In the MPC i like to use a last changed timestamp field as an etag

lo_property = lo_entity_type->create_property( iv_property_name = 'Updated' iv_abap_fieldname = 'UPDATED' ).

lo_property->set_type_edm_datetime( ).

lo_property->set_as_etag( ).

When accessing an entity the Etag will be present in both the HTTP header and payload responses and will look something similar to.

"etag":"W/\"datetime'2013-01-20T11%3A23%3A36'\""

During processing of a POST or a PUT/PATCH, the server will compute a new ETag by calling GET_ENTITY, in the example above by looking at the last UPDATED field. If the ETag entry provided by the client in the If-Match header does not match, the server will fail the request a 412 "Precondition Failed" or 428 "Precondition Required" response codes.

Note this is a very simple example. As I said at the top, this is one subject which can get complicated very quickly depending on your requirements. There are many different factors to consider, like the client, http communication, gateway/OData protocols, ABAP and the database etc. With investigation you can come up with a variety of derivations including using techniques like the Reservation Pattern, where the entity is locked for a limited time.

Hope it helps.

John P

SyambabuAllu
Contributor
0 Kudos

Hi John,

I understand the what you suggested regarding above question,but one thing why we should not go for pessimistic concurrency control using OData.

Thanks,

Syam

former_member182048
Active Contributor
0 Kudos

Hi Syam

Good question.

Have a look at constraints associated with Representational State Transfer (REST). In particular Stateless

The client–server communication is further constrained by no client context being stored on the server between request

REST enables intermediate processing by constraining messages to be self-descriptive: interaction is stateless between requests,  standard methods and media types are used to indicate semantics and exchange information, and responses explicitly indicate cacheability

Architectural Styles and the Design of Network-based Software Architectures - Section 5.3.1 Process ...

As you have already discovered Pessimistic Locking assumes application state.

Cheers

John  P


SyambabuAllu
Contributor
0 Kudos

Hi John,

Thanks for the information.

Regards,

Syam

Benedikt3
Participant
0 Kudos

Hi John,

Thank you for your reply,

the optimistic lock is a great thing, but not really useful for an real application….

For example, if an external staff is changing a maintenance order.

  1. external staff read the order with his mobile device
  2. external stuff adds (local) information
  3. office stuff edit the order at the desk (no way to notify the office staff about the external worker, in abap/sap)
  4. external stuff will save this edited order
  5. with optimistic lock -> horrible merging of conflicts on the device… 
  6. get the order again, merger
  7. try to save or back to 5…

REST is stateless okay, but that is not a contrast to a pessimistic lock.  Stateless don`t say:  "change an entity for a user request".   Stateless means more something like: “you don’t need a previous request to perform that request”.

And if somebody change the write access with an UPDATE  for an entity from all too just one specific. There is no REST concept conflict.


But back to my question, there is no build-in feature for that, right? So, I have to use the cross-lock mechanism  or something like to get a solution.

---

I will mark your answer as correct, because that is the best answer I could find....

qmacro
Developer Advocate
Developer Advocate
0 Kudos

Hi Benedikt

It's a very interesting subject, and your scenario is an example of a wider, more general 'offline app' problem for which neither optimistic or pessimistic locking alone is the only part of the solution. I for one would wonder at a system that would lock objects (pessimistically) in the system of record while a mobile app user had 'checked out' that object during the day, for example.

Ultimately it all boils down to design, and while we are used to pessimistic locking in a self-contained and realtime/online system (such as SAP), when we come to a much larger multi-app ecosystem such as the web, pessimistic locking can never work. John's (lovely detailed) answer explains this better than I can in this comment.

cheers

dj

former_member182048
Active Contributor
0 Kudos

Hi Beny

I agree with DJ, it comes down to design.

In a system which cannot count on distributed transactions, the management of uncertainty must be implemented in the business logic.

Life beyond Distributed Transactions - Section 6 Coping without ATOMicity.

Just letting you know that Gateway provides an Entity Subscription / Notification service. With this you can subscribe to entity changes or business events and have them pushed directly to your device. I have set this up (without SUP) and use it on various Android applications  when a Notification is sent back to my device depending on the scenario I can have the application either notify the user of the change or event, prompting them for action and or automatically sync back the phone with the back-end.

Using this service with your scenario (which i too read as offline)

3.Office staff edits the order at the desk.

3.1 Notification of change is syndicated to all users/systems who subscribe

4. External staff tries to save Order

4.1 The Order is dirty - business logic to resolve

I hope more people contribute to this discussion, it is a very interesting topic.

Cheers

JSP

former_member182670
Contributor
0 Kudos

Hi,

I know this thread is 1 year old but currently I'm looking for solutions for the same problem and there are no other discussion or guides on SCN for this topic.

I'd be interested if you decided to use Etags and optimistic concurrency for Odata service while keeping pessimistic control for sapgui user.

Answers (1)

Answers (1)

Former Member
0 Kudos

Interesting topic!

The etag scenario would not cover following scenario, right?

1. Mobile device user switches to "edit mode" for specific data

2. After that a sapgui users switches to "edit mode" too

3. Mobile user saves first -> successfully saved data in backend?

--> I wonder, because the sapgui user locked the data meanwhile, so it should not succeed

The etag scenario only is suitable, when sapgui users switch to "edit mode" first, and *after that* the mobile device tries to switch to "edit mode".

Maybe I didnt understand, but wouldnt there be a chance to somehow transfer the backend enqueue/dequeue status via Gateway as a response back to the device? Or set those flags in the backend when switching to edit mode on the mobile device?

former_member182048
Active Contributor
0 Kudos

Hi Sefan

Using Sales Orders as an example

1. Mobile device user switches to "edit mode" for specific data

2. After that a sapgui users switches to "edit mode" too

3. Mobile user saves first ->

3.1 Mobile user gets an error message saying "Sales Order locked by SAPGUI user"

"wouldnt there be a chance to somehow transfer the backend enqueue/dequeue status via Gateway as a response back to the device?"

This is where you would need to implement some business logic, you would want to avoid getting too chatty.

I am interested in hearing peoples ideas.

One thought I had would be in step 1 to call a Gateway function which triggers logic where the system initiates a time based reservation in the lock table on behalf of the mobile user, would be interested in hearing from a Basis person of how best to do this or alternatives.

Cheers

John P

kai_westerholz
Explorer
0 Kudos

Hi John,

I don't think a time based reservation is a solution. In my opinion this come with the same problems as using a pessimistic concurrency. As you mentioned earlier this comes down to the communication layer where you have a stateless communication. This is how REST works but for a reservation you need to know that an update request is mapped to such a reservation. In my eyes this generates some overhead and implementation for the coordination. Additionally I doubt the use of it. The advantage for the user is mostly combined with the time an object is reserved. But also the time for a reservation related to the performance. So as an example say the object is reserved for 120 seconds and is locked for others in that time. I think it is hard to get data, analyze it and act accordingly within two minutes. Of cause this is useful for very small tasks as an leave
request, but not for real applications in my eyes.

Apossible way to avoid some conflicts with optimistic concurrency is to change the way the ETag is used. In SAP systems and in your example above the last changed date is used for optimistic concurrency. But in most cases it is not useful to build mobile applications that view all of the data an object has. So the OData Service you implement is restricted to a small set of data. When calling the service the ETag is calculated as some kind of checksum over the used fields. In case of an update the Service checks the ETag against the newly calculated sum. This would not eliminate conflicts but it decreases the chance of it. The problem about this is how you calculate the checksum. As used for passwords it is still possible to have the same checksum with different data. Accepting the tiny chance there is still the problem of implementing the checksum. In addition I’m not sure how much complexity you can build in the calculation of the checksum.

What do you think about using a checksum based on the used fields?

Best regards,

Kai

former_member182048
Active Contributor
0 Kudos

Hi Kai

Thanks for the response.

wrt the Reservation Pattern, "In my eyes this generates some overhead and implementation for the coordination". I spent a couple of hours investigating a simple generic solution for this pattern, I eventually came to the same conclusion as you.

A checksum based on the used fields is a good idea, from memory there are a couple of different ways you could do this in ABAP.

Cheers

JSP

Former Member
0 Kudos

Hi ,

I just noticed a change since SAPUI5 v.1.9.1

https://sapui5.netweaver.ondemand.com/sdk/#docs/guide/ReleaseNotes.html

CHANGE: OData Write functionality now also supports the use of ETags for concurrency control.

I wonder if this offers new possibilities for our problem? What do you think, could you already test that feature?

Regards,

Stefan

former_member182048
Active Contributor
0 Kudos

Hi Stefan

Thanks a lot for the heads up. I hadn't seen that feature before in sapui5, really like how the 'If-Match' header is automatically applied if the entity has a tag, definitely a time saver. https://sapui5.netweaver.ondemand.com/sdk/resources/sap/ui/model/odata/ODataModel-dbg.js

I wonder if this offers new possibilities for our problem?

Its a start, sapui5 is currently more suited to online scenarios.

Hoping in future releases the ODataModel integrates to the jQuery.sap.storage module offering some offline capability (local/device/cloud etc). Also it would be nice to see support for some of the cache http headers and Gateways version of reference data caching

Cant help but think those features would come quicker if the project was made opensource.

Cheers

JSP