What are the two locking strategies in Hibernate?

There are two popular locking strategies that can be used in Hibernate:

  • Optimistic: In Optimistic locking we assume that multiple transactions can complete without affecting each other. So we let the transactions do their work without locking the resources initially.Just before the commit, we check if any of the resource has changed by another transaction, then we throw exception and rollback the transaction.
  • Pessimistic: In Pessimistic locking we assume that concurrent transactions will conflict while working with same resources. So a transaction has to first obtain lock on the resources it wants to update.The other transaction can proceed with same resource only after the lock has been released by previous transaction.