Carlos Jaime C. De Leon
Carlos Jaime C. De Leon

Reputation: 2906

Spring @Transactional(isolation=Isolation.READ_UNCOMMITTED) vs Spring Data @Lock(LockModeType.None)

Some systme info, it's using Spring, Spring Data, JPA and Hibernate.

My question is, what's the difference between the 2? See below:

//this uses plain JPA, and uses Spring's Transactional
@Transactional(isolation=Isolation.READ_UNCOMMITTED)
public List getData() {
   //code
}


//this uses Spring Data
@Lock(LockModeType.None)
public List getData() {
   //code
}

Upvotes: 1

Views: 3569

Answers (1)

Xstian
Xstian

Reputation: 8282

Locking and Transaction are two different things, but anyway...

This isolation level allows dirty reads. One transaction may see uncommitted changes made by some other transaction.

/**
 * A constant indicating that dirty reads, non-repeatable reads and phantom reads
 * can occur. This level allows a row changed by one transaction to be read by
 * another transaction before any changes in that row have been committed
 * (a "dirty read"). If any of the changes are rolled back, the second
 * transaction will have retrieved an invalid row.
 * @see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED
 */
READ_UNCOMMITTED

LockModeType

Lock modes can be specified by means of passing a LockModeType argument to one of the EntityManager methods that take locks (lock, find, or refresh) or to the Query.setLockMode() or TypedQuery.setLockMode() method.

Lock modes can be used to specify either optimistic or pessimistic locks.

So, as it said LockModeType.NONE is default for annotations (JPA, annotations left and right) I guess when you use EntityManager.find(Class, Object) the default LockModeType is used.

I suggest to you this link to see some examples to Pessimistic and Optimistic lock, and this link to see Transactional examples

Upvotes: 1

Related Questions