First Check
Commit to Help
Example Code
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
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.
First Check
Commit to Help
Example Code
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
LockModeTypesprovided by the Java Persistence API (API).Optimistic locking (
@Lock(LockModeType.OPTIMISTIC_FORCE_INCREMENT)):Pessimistic locking (
@Lock(LockModeType.<...>)):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.<...>:In SQLModel one could implement this functionality using Pydantic classes instead of the SQLAlchemy classes.
Wanted Code
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.