To put it simply:
Optimistic Concurrency means that when writing a database, it is assumed that there will be no conflict, and then it will be processed when there is a conflict.
Pessimistic concurrency means that errors always occur when writing data to the database. Therefore, we need to compare the Field Values Before updating.
Details:
Optimistic Concurrency: In optimistic concurrency control, data is not locked when users read data. When an update is executed, the system checks whether the data has been changed after another user has read the data. If another user updates the data, an error occurs. Generally, the user who receives the error message will roll back the transaction and start again. This method is mainly used in environments with less data competition and in which the cost of occasionally rolling back transactions exceeds the cost of locking data when reading data. Therefore, this method is called optimistic concurrency control.
Pessimistic concurrency: the locking system prevents users from modifying data in a way that affects other users. If a lock is applied because of the operation performed by the user, other users cannot perform operations that conflict with the lock until the lock owner releases the lock. This method is mainly used in environments where data competition is fierce, and when concurrent conflicts occur, the cost of using locks to protect data is lower than the cost of rollback transactions, therefore, this method is called pessimistic concurrency control.