I have some knowledge on JPA and DB locks (both optimistic and pessimistic) and transactions, how they works and what guarantees.
A TransactionRequiredException is thrown if there is no active transaction when lock is called because explicit locking requires an active transaction.
So my questions is: what is the use case when transaction (@Transactional in JPA case) is not enough and need explicitly use @Lock( LockModeType.PESSIMISTIC_*)?
I interested in exact pessimistic lock, and expect a use case like:
Alice and Bob simultaneously edit page on wikipedia, Alice start to edit a page and locked it..........................and transaction is redundant/not enough/low performance in this case ............................
I don't expect:
Use @Lock when you need just a lock
Use optimistic lock its better
Locking is used to prevent dirty reads (reading data that hasn't been committed) and non-repeatable reads (reading data that gets deleted by another transaction before the one reading is finished).