Spring @Transactional和Hibernate @LockMode注释是如何相关的? [英] How are the Spring @Transactional and the Hibernate @LockMode annotations related

查看:168
本文介绍了Spring @Transactional和Hibernate @LockMode注释是如何相关的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道事务和锁之间的关系。

更具体地说,Spring的 @Transactional 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



如果我没有在创建会话对象时指定任何锁,并使用 @Transactional readOnly 作为 false code>,我使用Pessimistic Concurrenct Control。



如果有人能告诉我(乐观/悲观)并发控制和交易。



谢谢,
Vivek

解决方案

首先,就像@ ck1已经提到的那样, @Transcational @LockMode 之间没有直接 / code>。



正如本文 @Transcational 用于标记RESOURCE_LOCAL或JTA事务的显式边界。你需要它的原因是因为执行每个数据库语句在事务性上下文中,并且,如果您未设置事务边界,则每个语句或自动提交都会获得一个事务。



开另一方面, @LockMode 用于设置显式锁定选项。如果你没有设置它,将使用隐式锁定机制:


  • 显式锁定在2PL和MVCC数据库引擎。如果您在Serializable上使用Repeatable Read,则在2PL引擎上的读取记录上获取共享锁。

  • 如果您定义了 @Version 隐式 strong>乐观锁定机制将被使用。



因此, @LockMode 用于显式设置锁定选项 ,您可以选择以下选项:



PESSIMISTIC 锁定模式将始终在与锁定实体关联的表行上获取数据库锁定。 OPTIMISTIC 锁定模式旨在为您提供一种即使在当前正在运行的持久性上下文中未更改实体版本的情况下也能实现实体版本的方法。这是一个非常有用的机制,当你需要使用其父实体版本协调多个子实体



我在这个答案中提供的链接,请花点时间,全部阅读,并且您将更详细地了解所有这些概念。


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

解决方案

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

As explained in this post, @Transcational is used to mark the explicit boundaries of a RESOURCE_LOCAL or JTA transaction. The reason why you need it it is because 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, @LockMode is for setting explicit locking options. If you don't set it, the implicit locking mechanisms will be used:

  • Explicit 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, @LockMode is for setting locking options explicitly, and you can have the following options:

The PESSIMISTIC lock modes will always acquire a database lock on the table row that is associated with the locked entity. 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.

这篇关于Spring @Transactional和Hibernate @LockMode注释是如何相关的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆