Pessimistic locking
By default, conflicts occur, and records are locked at the end of the query until unlocked at the end of the transaction; The principle is to use a lock in the database.
Optimistic locking
By default, no conflicts occur and data integrity is checked only when a transaction is committed; The implementation principle is the version number mechanism or CAS algorithm.
Usage scenarios
Use less optimistic locks for write operations and more pessimistic locks for write operations.