简体   繁体   English

乐观锁-Hibernate的并发问题

[英]Optimistic Lock - Concurrency issue with Hibernate

I am trying to understand what Optimistic Lock in Hibernate does and how is it to be used properly. 我试图了解Hibernate中的Optimistic Lock的作用以及如何正确使用它。

My first question is whether it only cares that an exception is thrown when one Thread tries to persist an Object while another has in the meantime already changed the state of the same Object , or does it care that both modifications are saved correctly? 我的第一个问题是,是否只在一个Thread尝试保留一个Object而另一个Thread同时已经更改了同一Object的状态时才抛出异常,还是在乎这两个修改是否正确保存? Is the last at least true when chains of Objects are updated and the parts which each Thread tries to alter are different? 当对象链更新且每个Thread尝试更改的部分不同时,最后至少是正确的吗?

I would like to present an example in order to make my question on a true scenario comprehensible. 我想举一个例子,以使我对一个真实情况的疑问可以理解。 It is a bad-designed database-model but it serves to depict the issue: 这是一个设计不良的database-model但可以用来描述问题:

Assuming one has the following Entities: 假设其中一个具有以下实体:

@Entity
public class Order{

@OneToMany
private User_Order user_Order;

}


@Entity
public class User_Order{

@OneToMany
private Product product;

}

In words an Order holds a number of User-Orders and each User-Order has a number of Products . 换句话说,一个Order包含多个User-Orders ,每个User-Order具有多个Products Assuming that each Thread excecutes the following code: 假设每个线程执行以下代码:

Order order = (Order) session.get(Order.class, 1);
//some other code...
Set<User_Order> userOrders = order.getUserOrder();
User_Order myUserOrder = //find my User_Order
List<Products> products = myUserOrder.getProducts();
//modify myUserOrder 
//modify products
session.update(order);

It is apparent that if a Thread updates the Object order , then the data which was persisted by other Threads return to its initial state, because the last Thread does not know anything about the update. 显然,如果一个线程更新了Object order ,那么其他线程所保留的数据将返回其初始状态,因为最后一个线程对更新一无所知。 (As already admitted, it is the result of a bad-designed database-model ) (正如已经承认的,这是错误设计的database-model

1) What is the best way to ensure concurrency safety in case that several Threads can excecute this code simultaneously ? 1)在多个Threads可以同时执行此代码的情况下,确保并发安全的最佳方法是什么? Can an Optimistic Lock be applied here? 可以在这里应用Optimistic Lock吗?

2) What is the answer to the previous question if one Thread (User) can modify also the data of which theoritically belong to other Users? 2)如果一个Thread (用户)也可以修改理论上属于其他用户的数据,那么对上一个问题的答案是什么?

When you work with database that supports transactions the problem of concurrent access moves away from java code (if you use database access tools correctly) and should be handled by database and locking strategy. 当您使用支持事务的数据库时,并发访问的问题将转移到Java代码之外(如果您正确使用了数据库访问工具),则应由数据库和锁定策略来处理。

I understand that you read this article for instance: http://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch05.html 我了解您阅读了以下文章,例如: http : //docs.jboss.org/hibernate/orm/4.0/devguide/zh-CN/html/ch05.html

If not - read it first. 如果没有,请先阅读。

The main idea is, that when you use hibernate, you communicate with database through Session object, which is not thread-safe and should be thread confined, which means - don't use it between threads, session per thread. 主要思想是,当您使用休眠模式时,您是通过Session对象与数据库进行通信的,该对象不是线程安全的,应该限制在线程范围内,这意味着-请勿在线程之间使用,每个线程都使用会话。 If you do that, great - all you need is is to decide which strategy will you use for locking - optimistic, or pessimistic locking. 如果这样做,那么太好了-您所需要的就是确定要使用哪种策略进行锁定-乐观锁定或悲观锁定。

Optimistic is more user friendly, because for instance all user can read and edit the data, but when two guys will at the same time edit the data, the first one wins, and the second one has to reenter his data. 乐观对用户更友好,因为例如所有用户都可以读取和编辑数据,但是当两个家伙同时编辑数据时,第一个赢得胜利,第二个必须重新输入数据。 It's ok, when you have a small form with 2 text boxes, but not really, when you have 5000 characters text area to fill. 可以,当您有一个带有2个文本框的小型表单时,但不是真的,当您要填充5000个字符的文本区域时,这不是真的。 In second case you want to use pessimistic locking and use the level of locking that suits your needs. 在第二种情况下,您要使用悲观锁定,并使用适合您需要的锁定级别。

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

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