gpt4 book ai didi

hibernate - Hibernate 中 session.lock 方法的目的是什么

转载 作者:行者123 更新时间:2023-12-04 02:06:02 28 4
gpt4 key购买 nike

我正在经历lock method hibernate 的。我没有得到我们试图通过这种方法实现的目标。

 p1  = (Person)session. get(Person.class,1);// person name is scott here
// here peson name got update to henry by anothet thread
session. lock(person, LockMode.READ) line 3
// still name is henry too

我尝试了上面的代码片段,但它没有从第 3 行的数据库中读取这个人。
文档说这可用于执行版本检查。

不确定它如何检查版本并在任何情况下帮助开发人员?

同样不知道是什么 session.lock(person, LockMode.None)将实现。文档说 LockMode.NONE用于简单地将 transient 实例与 session 重新关联。

不确定将 transient 实例与 session 重新关联是什么意思。一个简短的场景会有很大帮助吗?

最佳答案

不同的锁定模式旨在防止实体同时被修改和从多个来源读取,请参阅关于 pessimistic locking 的文档条目。详情。根据我的经验,很少需要这些,因为数据库隔离级别通常会根据需要处理锁定:

It is not intended that users spend much time worrying about locking strategies. It is usually enough to specify an isolation level for the JDBC connections and then simply let the database do all the work. However, advanced users may wish to obtain exclusive pessimistic locks or re-obtain locks at the start of a new transaction.



至于“将临时实例与 session 重新关联”(我实际上认为它们是指分离的实例?),请考虑下图( Hibernate entity lifecycle ):

Hibernate entity lifecycle

这是来自 Hibernate community documentation 的描述:

Hibernate defines and supports the following object states:

  • Transient - an object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application does not hold a reference anymore. Use the Hibernate Session to make an object persistent (and let Hibernate take care of the SQL statements that need to be executed for this transition).
  • Persistent - a persistent instance has a representation in the database and an identifier value. It might just have been saved or loaded, however, it is by definition in the scope of a Session. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes. Developers do not execute manual UPDATE statements, or DELETE statements when an object should be made transient.
  • Detached - a detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, of course, and the detached instance might even be modified in this state. A detached instance can be reattached to a new Session at a later point in time, making it (and all the modifications) persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e., a unit of work from the point of view of the user.

关于hibernate - Hibernate 中 session.lock 方法的目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8113301/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com