简体   繁体   English

Spring Hibernate悲观锁定

[英]Spring Hibernate Pessimistic locking

Some entities on my application needs to be locked during the scope of a certain transaction. 在特定事务范围内,我的应用程序上的某些实体需要锁定。 I need to lock all the entities that take part in the transaction and would't like to create special methods for locking the entities on my DAO. 我需要锁定参与交易的所有实体,并且不想创建用于锁定DAO上实体的特殊方法。 I am using spring's declarative transaction and set the isolation level for that particular method call, but when the data are fetched the generated SQL query, is a normal SELECT and not a SELECT XXX FOR UPDATE. 我正在使用spring的声明式事务并为该特定方法调用设置隔离级别,但是在提取数据时,将生成的SQL查询是普通的SELECT而不是SELECT XXX FOR UPDATE。 Do I have to manually specify the lock on each participating entity on the transaction on hibernate? 我是否必须在休眠状态下手动指定交易中每个参与实体的锁定? Shouldn't this be controlled from the transaction isolation? 不应该通过事务隔离来控制吗? When setting the lock mode to UPDATE on hibernate's session I can see the correct query, does that mean that I need to manually specify the lock even though I have already specified the isolation. 在休眠的会话上将锁定模式设置为UPDATE时,我可以看到正确的查询,这意味着即使我已经指定了隔离,我也需要手动指定锁定。

Best 最好

Yes. 是。 Isolation level and locking are different things, and for sure locks will not be controlled from the transaction isolation. 隔离级别和锁定是不同的东西,并且可以肯定的是,不会通过事务隔离来控制锁定。 Additionallky i would advice you not play with isolation level unless you have a very good reason. 另外,我建议您不要玩隔离级别的游戏,除非您有充分的理由。

You should use pessimistic locking when querying to apply SELECT ... FOR UPDATE on the selected entities. 查询时应使用悲观锁定,以将SELECT ... FOR UPDATE应用于所选实体。

This would help you 这对你有帮助

Spring Pessimistic locking 春季悲观锁定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM