I have read about transaction isolation levels. It is used to prevent parallel transaction executions errors. Its quite obvious. There are also locking modes available for entities. I understand how they work.
But I cant find the reason why I need locking? I have used transaction isolation levels already. Why do I have to use locking? Do isolation levels and locking make the same job?
I found that the @Transactional is used to ensure transaction on repository method or on a service method. @Lock is used on repository method to ensure locking of entity to provide isolation.
We can set the isolation level of a transaction by @Transactional::isolation. It has these five enumerations in Spring: DEFAULT, READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE.
A lock with LockModeType. PESSIMISTIC_WRITE can be used when querying data and there is a high likelihood of deadlock or update failure among concurrent updating transactions. The persistence implementation must support use of locks of type LockModeType.
Understanding Optimistic Locking While using it, each transaction that reads data holds the value of the version property. Before the transaction wants to make an update, it checks the version property again. If the value has changed in the meantime, an OptimisticLockException is thrown.
Introduction
There are different locking types and isolation levels. Some of the locking types (OPTIMISTIC*) are implemented on the JPA-level (eg. in EclipseLink or Hibernate), and other (PESSIMISTIC*) are delegated by the JPA-provider to the DB level.
Explanation
Isolation levels and locking are not the same, but they may intersect somewhere. If you have the SERIALIZED isolation level (which is performance-greedy), then you do not need any locking to do in JPA, as it is already done by the DB. On the other side, if you choose READ_COMMITTED, then you may need to make some locking, as the isolation level alone will not guarantee you e.g that the entry is not changed in the meanwhile by another transaction.
Both transaction isolation and JPA Entity locking are concurrency control mechanisms.
The transaction isolation is applied on a JDBC Connection level and the scope is the transaction life-cycle itself (you can't change the transaction isolation from your current running transactions). Modern databases allow you to use both 2PL (two-phase locking) isolation levels and MVCC ones (SNAPSHOT_ISOLATION or PostgreSQL isolation levels). In MVCC, readers do not block writers and writers do not block readers (only writers block writers).
The Java Persistence Locking API offers both database-level and application-level concurrency control, which can be split in two categories:
Explicit Optimistic lock modes:
The optimistic locking uses version checks in UPDATE/DELETE statements and fail on version mismatches.
Explicit pessimistic lock modes:
The pessimistic lock modes use a database-specific lock syntax to acquire read (shared) or write (exclusive) locks (eg. SELECT ... FOR UPDATE).
An explicit lock mode is suitable when you run on a lower-consistency isolation level (READ_COMMITTED) and you want to acquire locks whose scope are upgraded from query life-time to a transaction life-time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With