Search code examples
javagwtrequestfactory

Does GWT RequestFactory support implementation of optimistic concurrency control?


In a GWT app I present items that can be edited by users. Loading and saving the items is perfomed by using the GWT request factory. What I now want to achive is if two users concurrently edit an item that the user that saves first wins in the fashion of optimistic concurrency control. Meaning that when the second user saves his changes the request factory backend recognizes that the version or presence of the item stored in the backend has changed since it has been transfered to the client and the request factory/backend then somehow prevents the items from being updated/saved.

I tried to implement this in the service method that is used to save the items but this will not work because request factory hands in the items just retrieved from the backend with applied user's changes meaning the versions of these items are the current versions from the backend and a comparison pointless.

Are there any hooks in the request factory processing I coud leverage to achieve the requested behaviour? Any other ideas? Or do I have to use GWT-RPC instead...


Solution

  • No: http://code.google.com/p/google-web-toolkit/issues/detail?id=6046

    Until the proposed API is implemented (EntityLocator, in comment #1, but it's not clear to me how the version info could be reconstructed from its serialized form), you'll have to somehow send the version back to the server.
    As I said in the issue, this cannot be done by simply making the version property available in the proxy and setting it; but you could add another property: getting it would always return null (or similar nonexistent value), so that setting it on the client-side to the value of the "true" version property would always produce a change, which guaranties the value will be sent to the server as part of the "property diff"; and on the server-side, you could handle things either in the setter (when RequestFactory applies the "property diff" and calls the setter, if the value is different from the "true" version, then throw an exception) or in the service methods (compare the version sent from the client –which you'd get from a different getter than the one mapped on the client, as that one must always return null– to the "true" version of the object, and raise an error if they don't match).

    Something like:

    @ProxyFor(MyEntity.class)
    interface MyEntityProxy extends EntityProxy {
       String getServerVersion();
       String getClientVersion();
       void setClientVersion(String clientVersion);
       …
    }
    
    @Entity
    class MyEntity {
       private String clientVersion;
       @Version private String serverVersion;
    
       public String getServerVersion() { return serverVersion; }
       public String getClientVersion() { return null; }
       public void setClientVersion(String clientVersion) {
          this.clientVersion = clientVersion;
       }
    
       public void checkVersion() {
          if (Objects.equal(serverVersion, clientVersion)) {
             throw new OptimisticConcurrencyException();
          }
       }
    }
    

    Note that I haven't tested this, this is pure theory.