简体   繁体   English

JPA乐观锁版本处理 - 版本值应该被带到客户端还是?

[英]JPA optimistic lock version handling - version value should be carried onto client side or?

I'm wondering how to handle optimistic lock version property in entity class using JPA (toplink essentials) from server to client and vice versa. 我想知道如何使用JPA(toplink essentials)从服务器到客户端处理实体类中的乐观锁版本属性,反之亦然。

Here is the scenario. 这是场景。

  1. From browser user send request to the server asking for individual user info to edit. 从浏览器用户发送请求到服务器,要求编辑个人用户信息。

  2. Server processes the request and return the result to the browser. 服务器处理请求并将结果返回给浏览器。 The server code looks something like: 服务器代码类似于:

    EntityManager em = EmProvider.getInstance().getEntityManagerFactory().createEntityManager(); EntityManager em = EmProvider.getInstance()。getEntityManagerFactory()。createEntityManager();

    User u = (User)em.find(User.class, myUserId); 用户u =(用户)em.find(User.class,myUserId);

    return u; 回报你; //response back to the browser //回复浏览器

Here, my confusion is User table has "version" column for optimistic locking . 在这里,我的困惑是User表有"version"列用于optimistic locking

That means value of version field is also sent back to the client even though the client (me or anyone) would never use it . 这意味着版本字段的值也会被发送回客户端,即使客户端(我或任何人)永远不会使用它 The version field is to be used in server side code. 版本字段将用于服务器端代码。

So is it correct to send version number to client? 那么将版本号发送给客户端是否正确? Because otherwise I can't figure out how to check version number in case user clicks on "UPDATE" button on web page with modified data. 因为否则我无法弄清楚如何检查版本号,以防用户点击带有修改数据的网页上的“更新”按钮。

Please let me know if you need more clarification. 如果您需要更多说明,请告诉我。

Yes, you would send the version number to the client, so that he can later send it back to the server (together with the changes he wants to do to the entity), who can use it to check for conflicting updates. 是的,您可以将版本号发送给客户端,以便他以后可以将其发送回服务器(连同他想要对实体进行的更改),谁可以使用它来检查冲突的更新。

How else would the server know against which version to check? 服务器如何知道要检查哪个版本? (One could also put the number in a server-side session, but that is basically a variation on the same theme). (也可以将数字放在服务器端会话中,但这基本上是同一主题的变体)。 The point is that when you "check out" the version to edit, you carry the version number from that point in time along. 关键是,当您“签出”要编辑的版本时,您将携带该时间点的版本号。

That means value of version field is also sent back to the client even though the client (me or anyone) would never use it. 这意味着版本字段的值也会被发送回客户端,即使客户端(我或任何人)永远不会使用它。

Well, you could use it on the client. 好吧,你可以在客户端上使用它。 For example if the editing operation takes a long time, the client could poll once in a while if the entity has concurrently been updated, and then alert the user about it. 例如,如果编辑操作需要很长时间,则客户端可以偶尔轮询一次实体是否已同时更新,然后向用户提醒它。 (Similar to the "A new answer has been posted" message here on Stackoverflow). (类似于Stackoverflow上的“已发布新答案”消息)。

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

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