Skip to main content
Tweeted twitter.com/StackSoftEng/status/1217416299845947392
edited tags
Link
Christophe
  • 82.3k
  • 11
  • 136
  • 202
deleted 4 characters in body
Source Link

Considering that:

  • when using the repository pattern you deal with entities - those are the atomic units you persist (regardless of Hibernate or "manually")
  • when changing an entity you save it as a whole (there's no set or increment field)
  • multiple application instances might be running
  • the savesame entity might be fetched/changed/saved at the same timeconcurrently by different application instances

Won't this lead to bad data in the end? Consider the use case of "let me save the amount of password retries in the user entity". Won't that be problematic if an attacker launches many multiple client requests to login? Instance A fetches entity, instance B fetches same entity, instance A changes/saves it, instance B changes/saves the original and savesentity unknowingly of AA's change/save.

Considering that:

  • when using the repository pattern you deal with entities - those are the atomic units you persist (regardless of Hibernate or "manually")
  • when changing an entity you save it as a whole (there's no set or increment field)
  • multiple application instances might be running
  • the save entity might be fetched/changed/saved at the same time by different application instances

Won't this lead to bad data in the end? Consider the use case of "let me save the amount of password retries in the user entity". Won't that be problematic if an attacker launches many multiple client requests to login? Instance A fetches, instance B fetches, instance A changes/saves, instance B changes the original and saves unknowingly of A.

Considering that:

  • when using the repository pattern you deal with entities - those are the atomic units you persist (regardless of Hibernate or "manually")
  • when changing an entity you save it as a whole (there's no set or increment field)
  • multiple application instances might be running
  • the same entity might be fetched/changed/saved concurrently by different application instances

Won't this lead to bad data in the end? Consider the use case of "let me save the amount of password retries in the user entity". Won't that be problematic if an attacker launches many multiple client requests to login? Instance A fetches entity, instance B fetches same entity, instance A changes/saves it, instance B changes/saves the original entity unknowingly of A's change/save.

Source Link

How to prevent concurrency problems when using the repository pattern?

Considering that:

  • when using the repository pattern you deal with entities - those are the atomic units you persist (regardless of Hibernate or "manually")
  • when changing an entity you save it as a whole (there's no set or increment field)
  • multiple application instances might be running
  • the save entity might be fetched/changed/saved at the same time by different application instances

Won't this lead to bad data in the end? Consider the use case of "let me save the amount of password retries in the user entity". Won't that be problematic if an attacker launches many multiple client requests to login? Instance A fetches, instance B fetches, instance A changes/saves, instance B changes the original and saves unknowingly of A.