简体   繁体   English

如何防止两个用户对 web 应用程序中的数据记录执行相同的操作?

[英]How to prevent two users from taking same action on a data record in a web application?

We have a web application backed by spring framework which displays list of records to the users.我们有一个由 spring 框架支持的 web 应用程序,该框架向用户显示记录列表。 We have a requirement where-in we want to restrict single record being actioned from two users at the same time.我们有一个要求,我们希望限制两个用户同时操作单个记录。 ie if multiple users are trying to action the same record, request for user which action first is processed rest all should not be able to process it.即,如果多个用户尝试对同一记录执行操作,则请求首先处理哪个操作的用户 rest 都应该无法处理它。

We have looked at locking technique (optimistic locking) but not sure if this is the correct or only approach for achieving this functionality.我们已经研究了锁定技术(乐观锁定),但不确定这是否是实现此功能的正确方法或唯一方法。

Also, if optimistic locking is the correct approach, any pointers in that direction would be appreciated.此外,如果乐观锁定是正确的方法,那么任何指向该方向的指针都会受到赞赏。

Optimistic Locking is indeed the usual technique to solve this problem, and it does exactly you ask for: If several users want to perform conflicting actions at the same time, only the first such action succeeds, while all others are rejected. Optimistic Locking 确实是解决这个问题的常用技术,它完全符合您的要求:如果多个用户想要同时执行冲突的操作,那么只有第一个这样的操作成功,而其他所有操作都被拒绝。 Combining this with informing the other users that their request failed because somebody else was faster, and giving them the option to see what the other user did allows them to reconcile their changes, and try again if necessary.将这一点与通知其他用户他们的请求失败,因为其他人更快,并让他们选择查看其他用户所做的操作相结合,可以让他们协调他们的更改,并在必要时重试。

The advantage of Optimistic Locking is that it only springs into action in the event of an actual conflict.乐观锁定的优点是它仅在发生实际冲突时才会生效。 This is optimal if conflicts are rare, and that's why this technique is called "optimistic" locking.如果冲突很少,这是最佳的,这就是为什么这种技术被称为“乐观”锁定。

The disadvantage of Optimistic Locking is that it discards user work in the event of a conflict.乐观锁定的缺点是它会在发生冲突时丢弃用户工作。 If conflict is frequent, and the work being submitted significant, simply discarding that work may be too wasteful.如果冲突频繁,并且提交的工作很重要,那么简单地丢弃该工作可能太浪费了。 Also, sometimes the work has side effects beyond updating a database that must occur only once.此外,有时这项工作除了更新必须只发生一次的数据库之外,还会产生副作用。 For instance, the work may involve sending a letter or email, which can not be recalled in case of an optimistic locking failure.例如,该工作可能涉及发送一封信或 email,在乐观锁定失败的情况下无法召回。

In these cases, you'll want to prevent users from starting conflicting work rather than waiting until they wish to save their work.在这些情况下,您需要防止用户开始有冲突的工作,而不是等到他们希望保存他们的工作。 This is usually accomplished by allowing a user to take ownership of an issue at the application level before starting the actual work.这通常是通过允许用户在开始实际工作之前在应用程序级别获得问题的所有权来实现的。 Combined with ensuring that only a single user can take ownership at the same time (through Optimistic Locking), this prevents conflicting work from being started.结合确保只有一个用户可以同时拥有所有权(通过乐观锁定),这可以防止开始冲突的工作。 The disadvantage of this technique is that the user is required to take an additional action even if there turns out to be no conflict, so this optimizes for cases where conflict is expensive or frequent.这种技术的缺点是即使结果没有冲突,用户也需要采取额外的行动,因此这优化了冲突代价高昂或频繁发生的情况。

As for how to use optimistic locking, there are plenty of good tutorials on the internet.至于如何使用乐观锁,网上有很多不错的教程。 I'd start by checking the reference manuals of Spring and the JPA implementation you use.我首先检查您使用的 Spring 和 JPA 实现的参考手册。

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

相关问题 如何处理多个用户更新Web应用程序中的同一记录(Spring MVC和DB oracle pl / sql) - how to Handle multiple users update a same record in web application (Spring mvc & DB oracle pl/sql) 防止两个用户从单独的会话中提交相同的表单数据 - Prevent two users from submitting the same form data from separate sessions 航空公司系统如何防止两个用户同时预订同一个座位? - How does an airline system prevent two users from booking the same seat at the same time? 防止用户打开多个windows到同一个web app - Prevent users from opening multiple windows to the same web app 如何防止用户修改其他用户的数据? - How to prevent users from modifying other users' data? 如何防止同一应用程序并行Java Web Start下载? - How to prevent parallel Java Web Start downloads of the same application? 相同的应用程序部署在两个不同的服务器中,但是从数据库中获取相同的数据。 如何同步数据库数据 - Same application deployed in two different servers but fetching same data from DB. How to synchronize DB data 多个用户在同一浏览器上登录Web应用程序 - Multiple users login to web application on same browser 阻止用户编辑同一记录 - Prevent user from editing the same record 防止多个用户执行相同的操作 - Preventing multiple users from doing the same action
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM