如何确保休眠版本号在javascript客户端中保持不变? [英] How to ensure that the hibernate version number stays immutable in a javascript client?

查看:80
本文介绍了如何确保休眠版本号在javascript客户端中保持不变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Java和Hibernate的Rest后端,我使用了带有版本属性的Optimistic Locking. 对于并发控制,此版本属性必须转到客户端,然后再通过发布请求转到服务器. 但是,在javascript客户端中,我无法控制此属性的完整性,例如:

I have a Rest backend with Java and Hibernate and im using Optimistic Locking with the version property. For concurrency control, this version property must go to the client and again to the server with post request. But, in a javascript client i lose control over the integrity of this property, for example:

  • 客户端"A"请求资源1​​.
  • 客户端"B"请求相同的资源.
  • 服务器使用资源1版本1对两个客户端进行响应(在它们各自的响应中)
  • 客户端"A"修改资源并发布帖子以保存更改.
  • 服务器处理发布请求,然后Hibernate检查版本号. 1 = 1,因此资源已修改.
  • 客户端B使用javascript进行播放,并将version属性修改为2并发出发布请求. 服务器处理发布请求,现在2 = 2,因此资源被修改,客户端A所做的更改也将丢失.
  • A client "A" requests the resource 1.
  • A client "B" requests the same resource.
  • The server responds with resource 1 version 1 to both clients (in their respective responses)
  • The client "A" modifies the resource and makes a post to save the changes.
  • The server processes the post request and Hibernate checks version number. 1 = 1 so the resource is modified.
  • The client B plays with javascript and modifies the version property to 2 and makes a post request. The server processes the post request and now 2 = 2, so the resource is modified and the changes made by client A are lost.

请记住,这是一个Rest后端,因此我无法在服务器中保存已交付给每个用户的对象的版本号.

Remember that is a Rest backend, therefore i can't save in the server the version number of the objects that were delivered to each user, o something like that.

因此,如果这是我认为是有效的问题,我如何确保版本在客户端中保持不变?

So, if this is a valid question like i think it is, how can i ensure that the version stays immutable in the client?

推荐答案

我个人会重新考虑这样做,因为我发现客户这样做的动机很少甚至没有动机,并且根据定义,您无法控制客户向其发送的内容您的REST服务.

Personally, I would rethink doing this, as I see little to no motivations for a client to do that and by definition you can NOT control what a client sends to your REST service.

但是,如果您确实有业务案例,那么一个半解决方案是将version属性设置为GUID.这样,您只有在拥有最新的GUID(客户端B不会拥有)的情况下才可以提交,这会使版本控制情况更加复杂,因为在请求成功之前,您不能仅增加1.

However, if you really do have a business case, then one semi-solution is to make the version property a GUID. That way, you can only submit if you have the newest GUID, which client B would not have, and it makes the versioning case more complicated because you cannot just increment by 1 until the request succeeds.

但是,即使您那里并不完全安全,因为从理论上讲,客户端B可以单独发出请求以从资源中获取更新的GUID,然后使用更新的GUID重新提交失败的请求.

However, even there you are not entirely safe, as client B could theoretically do a separate request to get the updated GUID from the resource and then resubmit the failed request with the updated GUID.

但是,由于客户端B可以对更新的版本进行编辑以还原客户端A所做的操作并添加自己的操作,因此,这样做基本上是相同的,并且由于他们有权这样做,因此我们回到询问我们自己,为什么我们首先要担心这种情况.

But since client B could have done an edit on the updated version to revert what client A did and add their own, this is essentially the same as doing that, and since they have authority to do that, we are back to asking ourselves why we are worrying about that case in the first place again.

这篇关于如何确保休眠版本号在javascript客户端中保持不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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