Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are the Spring @Transactional and the Hibernate @LockMode annotations related

I wish to know the relation between transactions and locks.

To be more specific, how is Spring's @Transactional related to Hibernate's LockMode. https://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch05.html. http://docs.spring.io/autorepo/docs/spring/4.2.x/spring-framework-reference/html/transaction.html

If I dont specify any lock while creating Session Object, and use @Transactional with readOnly as false, am I using Pessimistic Concurrenct Control.

It would be a great help, if anyone can tell me the relation between (Optimistic/Pessimistic) Concurrency Control and Transactions.

Thank you, Vivek

like image 312
vivek4348 Avatar asked Jun 23 '16 01:06

vivek4348


People also ask

What is the difference between Spring transaction and hibernate transaction?

Hibernate deals with database specific transactions, whereas spring provides a general transaction management service. @Transactional is a nice way of configuring transaction management behaviour.

Why @transactional annotation is used in Spring?

So when you annotate a method with @Transactional , Spring dynamically creates a proxy that implements the same interface(s) as the class you're annotating. And when clients make calls into your object, the calls are intercepted and the behaviors injected via the proxy mechanism.

Where does the @transactional annotation belong?

The @Transactional annotation belongs to the Service layer because it is the Service layer's responsibility to define the transaction boundaries.

What does @transactional do in hibernate?

Transactions means all or nothing. If there is an exception thrown somewhere in the method, changes are not persisted in the database. Something called rollback happens. If you don't specify @Transactional , each DB call will be in a different transaction.


1 Answers

There is no direct relationship between @Transactional and @LockMode annotations.

@Transactional is used to mark the explicit boundaries of a RESOURCE_LOCAL or JTA transaction. The reason why you need it is that every database statement executes in a transactional context, and, if you don't set the transaction boundaries, you'll get one transaction per statement or auto-commit.

On the other hand, @LockModeType is for setting explicit locking options. If you don't set it, the implicit locking mechanisms will be used:

  • Implicit locks are acquired on every modified row on both 2PL and MVCC database engines. Shared locks are acquired on read records on 2PL engines if you use Repeatable Read on Serializable.
  • If you defined a @Version property, the implicit optimistic locking mechanism will be used.

So, @LockModeType is for setting locking options explicitly, and you can have the following options:

  • LockModeType.PESSIMISTIC_READ
  • LockModeType.PESSIMISTIC_WRITE

The PESSIMISTIC lock modes will always acquire a database lock on the table row that is associated with the locked entity.

There are also explicit optimistic lock strategies:

  • LockModeType.OPTIMISTIC
  • LockModeType.OPTIMISTIC_FORCE_INCREMENT
  • LockModeType.PESSIMISTIC_FORCE_INCREMENT

The OPTIMISTIC lock modes are meant to give you a way of bumping up an entity version even if the entity hasn't changed in the currently running Persistence Context. This is a very useful mechanism when you need to coordinate multiple child entities using their parent entity version.

There are lots of examples in the links that I provided in this answer, so take your time, read them all, and you'll understand all these concepts in greater detail.

like image 106
Vlad Mihalcea Avatar answered Sep 26 '22 12:09

Vlad Mihalcea