Hibernate中session.lock方法的目的是什么? [英] what is the purpose of session.lock method in Hibernate

查看:261
本文介绍了Hibernate中session.lock方法的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览锁定方法

休眠。我没有得到我们想通过这种方法实现的目标。

  p1 =(人)会话。 get(Person.class,1); //人名是scott here 
//这里peson名称通过anothet thread
session获得更新给henry。 lock(person,LockMode.READ)第3行
//仍然是henry的名字

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



不知道它是如何检查版本,并帮助开发人员在任何情况下?同样不知道什么 session.lock(person,LockMode.None) 将实现。文档说明 LockMode.NONE 用于简单地将临时实例与会话重新关联。



不确定通过重新关联一个临时实例和会话意味着什么?一个简短的场景会有很大的帮助吗?

解决方案

不同的锁模式用于防止实体被修改和读取同时从多个来源获取有关悲观锁定了解详情。根据我的经验,这些很少需要,因为数据库隔离级别通常需要根据需要进行锁定:


它并不是用户花费很多时间担心锁定
策略。通常为
JDBC连接指定隔离级别就足够了,然后让数据库完成所有工作。
但是,高级用户可能希望获得独占的悲观锁
或在新交易开始时重新获得锁。


至于重新关联暂时实例与会话(我实际上认为它们是指分离的实例?),请考虑以下图片( Hibernate entity lifecycle ): .stack.imgur.com / 61dYc.pngalt =Hibernate实体生命周期>



这是 Hibernate社区文档


Hibernate定义并支持以下对象状态:


  • 瞬态 - 如果对象具有暂态刚刚使用新操作符实例化
    ator,并且它与Hibernate
    Session没有关联。它在数据库中没有持久表示,并且没有赋值
    标识符值。如果应用程序不再持有
    引用,那么垃圾收集器将销毁
    的瞬态实例。使用Hibernate Session使对象
    持久化(并且让Hibernate处理需要为此转换执行
    的SQL语句)。

  • 持久性 - 持久
    实例在数据库中具有表示形式和标识符值。
    它可能刚刚被保存或加载,然而,它在会话范围内定义为
    。 Hibernate将检测对
    所做的任何更改,这些更改是持久状态的对象,并在工作单元完成时将状态与
    数据库同步。开发人员不会执行
    手动UPDATE语句,或者当对象应该
    变为瞬态时执行DELETE语句。

  • 分离 - 分离的实例是
    持久化的对象,但其Session已关闭。当然,引用
    对象仍然有效,并且分离的实例可能
    甚至可以在此状态下进行修改。一个分离的实例可以在稍后的时间点重新附加
    到一个新的会话中,使得它(和所有的
    修改)再次持久。此功能可为需要用户思考时间的长时间运行工作单元启用编程
    模型。我们
    称他们为应用程序事务,即从用户的
    的角度来看一个工作单元。


I am going through lock method of hibernate. I did not get what we are trying to achieve through this method.

 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

I tried above code snippet but it did not read the person from database at line 3. Documentation says that This may be used to perform a version check.

Not sure how does it check version and helps the developer in any scenario?

Similarly not sure what session.lock(person, LockMode.None) will achieve. Documentation says LockMode.NONE is used to to simply re-associate a transient instance with a session.

Not sure what does it mean by reassociating a transient instance with session. A brief scenario will be a great help?

解决方案

The different lock-modes are meant for preventing the entity from being modified and read from multiple sources simultaneously, see the documentation entry about pessimistic locking for details. In my experience, these are rarely needed, as the database isolation level usually takes care of locking as needed:

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.

As for the "reassociating a transient instance with the session" (I actually think they mean detached instance?), consider the following picture (Hibernate entity lifecycle):

This is the description from 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中session.lock方法的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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