0

I am working with Hybris and stumbled over this problem a few times:

Imagine a situation in which multiple threads (maybe HTTP threads, maybe background threads) are reading/writing the same Database item (maybe in a clustered environment). How do I avoid concurrency issues? What are the options?

Nomade
  • 1,760
  • 2
  • 18
  • 33
Johannes von Zmuda
  • 1,814
  • 8
  • 14
  • 3
    Try this post, it may have answers for your question : https://www.stackextend.com/hybris/control-concurrent-access-to-models-in-hybris/ – Mouad EL Fakir Feb 21 '18 at 14:55

4 Answers4

4

Have a look at database locking:

  • Optimistic Locking: Every item contains a field version. On every update of the Item the version will be checked. You can only update the item if the version of the item you loaded before matches the version of the item in the database. On success the version will increase.
  • Pessimistic Locking: When a process intends to update an item it is locked so that no other process can change it until the process releases the lock
Mehtrick
  • 518
  • 3
  • 12
0

Well my first idea is that if you implemente a version value, in the global thread variable's environment, each time you access to that item you change its version value (for example it was 0001 you access and now it is 0002), so in that chase if your services are acting on the same item if the version that he is working (ex. version 0001) is not the same as the version in saved in the server (ex. version 0002).

I hope you could understand the idea.

Drexpp
  • 28
  • 1
  • 6
0

In this case, and using hybris ORM implementation that use a cache memory, I think that you can:

Server side : Adapte your business logic to support concurrency, and use cache eviction to get updated data.

Database side : Use database locking, but that might have a real performance issue in your system. For more about Database locking see this.

Nomade
  • 1,760
  • 2
  • 18
  • 33
-1

You can LOCK TABLEin SQL to prevent changes while a process makes INSERTS.

Carlos López Marí
  • 1,432
  • 3
  • 18
  • 45