How Spring @Transactional and Hibernate @LockMode annotations are related

I want to know the relationship between transactions and locks.

To be more specific, how Spring @Transactional is related to Hibernate 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 don't specify any lock when creating the session object and use @Transactional with readOnly as false , will I use pessimistic counter-correspondent management.

It would be very helpful if someone could tell me about the relationship between the (optimistic / pessimistic) Concurrency Management and transactions.

Thanks, Vivec

+5
source share
3 answers

First of all, as @ ck1 was already mentioned, there is no direct relationship between @Transcational and @LockMode .

As explained in this post , @Transcational used to mark the explicit boundaries of a RESOURCE_LOCAL or JTA transaction. The reason you need this is because each database statement is executed in a transactional context , and if you do not set transaction boundaries, you will get one transaction per statement or automatic commit.

@LockMode , @LockMode other hand, @LockMode designed to set explicit lock options. If you do not install it, implicit locking mechanisms will be used:

  • Explicit locks are obtained in each modified row for both two databases and MVPL databases. General locks are acquired when reading records on 2PL models if you use Repeatable reading in Serializable.
  • If you defined the @Version property, an implicit optimistic locking mechanism will be used.

So, @LockMode designed to set the lock options explicitly , and you may have the following options:

PESSIMISTIC lock PESSIMISTIC will always receive a database lock in the row of the table that is associated with the locked object. OPTIMISTIC locking OPTIMISTIC are designed to give you the opportunity to raise the version of an entity, even if the object has not changed in the current launch of Conistence Context. This is a very useful mechanism when you need to coordinate multiple child objects using the parent entity version .

There are many examples in the links I provided in this answer, so do not rush, read all of them, and you will understand all these concepts in more detail.

+7
source

Spring @Transactional and Hibernate LockMode class are different.

Spring Transaction Management

@Transactional is Spring's annotation for declarative transaction management, that is, determining which SQL statements are executed together within a database transaction. Using the readOnly attribute allows Spring to throw an exception if you try to insert rows within a read-only transaction, for example.

In regards to locking, most likely you will use a read / write transaction ( readOnly = false ), because you will try to modify the data.

Pessimistic blocking

LockMode used for pessimistic locking, for example. LockMode.UPGRADE actually executes the SELECT...FOR UPDATE and locks the row in the database corresponding to the entity.

Pessimistic locking assumes that parallel transactions will conflict with each other and requires that resources be locked after they are read and only unlocked after the application has finished using data.

Optimistic block

The optimistic concurrency control in Hibernate typically uses the version column or timestamp in the database. The idea here is that if several transactions try to change the row at the same time, all but the first committed transaction will find that the version number has changed and rolled back.

Optimistic locking suggests that multiple transactions can complete without affecting each other, and therefore transactions can continue without locking the data that they affect. Prior to completing a transaction, each transaction verifies that no other transaction has changed its data. If the check shows inconsistent modifications, transaction completion.

Quotes above: https://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch05.html

+1
source

@ user1724103-The following site explains usage for LockMode

https://vladmihalcea.com/hibernate-locking-patterns-how-does-optimistic-lock-mode-work/

Hope this helps

+1
source

All Articles