Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA difference between transaction isolation and entity locking

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?

like image 244
bossman Avatar asked Jun 11 '15 09:06

bossman


People also ask

Does @transactional lock?

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.

What are the types of isolation levels described in Spring data JPA?

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.

What is LockModeType Pessimistic_write?

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.

What is optimistic locking JPA?

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.


2 Answers

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.

like image 37
V G Avatar answered Oct 31 '22 00:10

V G


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:

  1. Explicit Optimistic lock modes:

    • OPTIMISTIC
    • OPTIMISTIC_FORCE_INCREMENT
    • PESSIMISTIC_FORCE_INCREMENT

The optimistic locking uses version checks in UPDATE/DELETE statements and fail on version mismatches.

  1. Explicit pessimistic lock modes:

    • PESSIMISTIC_READ
    • PESSIMISTIC_WRITE

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.

like image 152
Vlad Mihalcea Avatar answered Oct 31 '22 00:10

Vlad Mihalcea