10

I'm using Spring Framework and Spring Data and I need to lock specific row in a database table while processing is done, so nobody else can access the data and change it until the process is finished. At the same time, I'm running multiple instances of the same application.

Will the usage of PESSIMISTIC_WRITE lock the specific row in a database in a such way that, when one application instance accesses that row, it will be inaccessible to other instances of the same application which could try to access the same row from the database at the same time?

There is a question discussing the differences between lock types, PESSIMISTIC_READ vs. PESSIMISTIC_WRITE. To be clear, I'm not asking for the differences between them, I'm asking for the scope of the PESSIMISTIC_WRITE and how will obtaining that lock from one application instance affect other applications instances which could try to read and update the same row in the database table.

dplesa
  • 1,355
  • 6
  • 23
  • 56
  • 1
    Possible duplicate of [What's the difference between PESSIMISTIC\_READ and PESSIMISTIC\_WRITE in JPA?](https://stackoverflow.com/questions/1657124/whats-the-difference-between-pessimistic-read-and-pessimistic-write-in-jpa) – K.Nicholas Dec 20 '18 at 04:05

1 Answers1

13

I have tested this use case and the answer is that the usage of PESSIMISTIC_WRITE will lock the specific row in a database in a such way that, when one application instance accesses that row, it will be inaccessible to other instances of the same application which could try to access the same row from the database at the same time.

dplesa
  • 1,355
  • 6
  • 23
  • 56