I'm wondering how to handle optimistic lock version property in entity class using JPA (toplink essentials) from server to client and vice versa.
Here is the scenario.
From browser user send request to the server asking for individual user info to edit.
Server processes the request and return the result to the browser. The server code looks something like:
EntityManager em = EmProvider.getInstance().getEntityManagerFactory().createEntityManager();
User u = (User)em.find(User.class, myUserId);
return u; //response back to the browser
Here, my confusion is User table has "version"
column for 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).