sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

Locking mechanisms for preventing data integrity issues in concurrent data access scenarios

Open fkromer opened this issue 3 years ago • 4 comments
trafficstars

First Check

  • [X] I added a very descriptive title to this issue.
  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the SQLModel documentation, with the integrated search.
  • [X] I already searched in Google "How to X in SQLModel" and didn't find any information.
  • [X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [X] I already checked if it is not related to SQLModel but to Pydantic.
  • [X] I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

n.a.

Description

So far SQLModel does not provide mechanisms for preventing data integrity issues in concurrent data access scenarios out of the box.

Wanted Solution

It would be great to have something similar like LockModeTypes provided by the Java Persistence API (API).

Optimistic locking (@Lock(LockModeType.OPTIMISTIC_FORCE_INCREMENT)):

  • OPTIMISTIC_FORCE_INCREMENT - Optimistic lock, with version update on the database level.

Pessimistic locking (@Lock(LockModeType.<...>)):

  • PESSIMISTIC_READ - acquire a shared lock, and the locked entity cannot be changed before a transaction commit.
  • PESSIMISTIC_WRITE - acquire an exclusive lock, and the locked entity can be changed.
  • PESSIMISTIC_FORCE_INCREMENT - acquire an exclusive lock and update the version column, the locked entity can be changed

When using OPTIMISTIC_FORCE_INCREMENT based optimistic locking and pessimistic locking table rows are locked at the database level. So this should be in the scope of SQLModel.

Another option would be to provide optimistic locking on the class instance level instead of the database level similar to the options available in Java. Hibernate provides e.g. optimistic locking via @OptimisticLocking(type = OptimisticLockType.<...>:

  • ALL - perform locking based on all fields
  • DIRTY - perform locking based on only changed fields
  • VERSION - perform locking using a dedicated version column

In SQLModel one could implement this functionality using Pydantic classes instead of the SQLAlchemy classes.

Wanted Code

n.a.

Alternatives

Reinvent the wheel over and over again :smil

Operating System

Other

Operating System Details

n.a.

SQLModel Version

n.a.

Python Version

n.a.

Additional Context

The article Optimistic and Pessimistic Locking in JPA is a nice resource about what types of locking is provided by and used in JPA.

fkromer avatar Apr 19 '22 19:04 fkromer

I second that. Writing to the same object in concurrent threads currently leads to a ROLLBACK. I have no way to catch a concurrent write and wait for the operation to finish before starting the other.

pitwegner avatar Oct 17 '23 11:10 pitwegner

Any update ?

ahmedazizkhelifi avatar Feb 13 '24 12:02 ahmedazizkhelifi