DoydoQuery v2
English
English
  • Introduction
  • Quickstart
  • API
    • CRUD
    • Association Service
  • Complex Query
  • Object Concepts
    • Query Object
      • PageQuery
      • Predicate-Suffix Field
      • Logic-Suffix Field
      • Subquery Field
      • E-R Query Field
      • Custom Condition Field
    • Entity Object
      • Enum Column
      • Foreign Key
    • Patch Object
    • View Object
      • Column Mapping
      • Natural Join
  • Having Object
  • Advanced
    • Dialect
    • Pessimistic lock
  • Optimistic Lock
  • Web
    • Configuration
    • Controller
    • Service
    • Cache
    • Sorting
    • Validation
    • User ID injection
Powered by GitBook
On this page
  • Shared lock
  • Exclusive lock

Was this helpful?

  1. Advanced

Pessimistic lock

Shared lock

To acquire a shared lock on the queried rows, assign PESSIMISTIC_READ to the lockMode field of the PageQuery class. This will generate an SQL statement with a FOR SHARE clause, allowing other transactions to read the same rows but preventing them from updating or deleting them until the current transaction is complete.

UserQuery testQuery = UserQuery.builder().id(1).lockMode(LockMode.PESSIMISTIC_READ).build();
List<UserEntity> lockedUsers = userService.query(userQuery);
// SQL: SELECT * FROM t_user t WHERE id = ? FOR SHARE

Exclusive lock

To acquire an exclusive lock on queried rows, assign PESSIMISTIC_WRITE to the lockMode field of the PageQuery class. This appends a FOR UPDATE clause to the SQL query, locking the selected rows for the duration of the transaction and preventing concurrent access by other transactions.

UserQuery testQuery = UserQuery.builder().id(1).lockMode(LockMode.PESSIMISTIC_WRITE).build();
List<UserEntity> lockedUsers = userService.query(userQuery);
// SQL: SELECT * FROM t_user t WHERE id = ? FOR UPDATE
PreviousDialectNextOptimistic Lock

Last updated 16 days ago

Was this helpful?