views:

157

answers:

1

Perhaps I'm missing something simple, as I believe that this is a common scenario...

When I am working with an object retrieved from the datastore, I want to detect any changes to the object and update the memcache. In a non-JDO scenario this would be easy, as any data logic layer would intercept all updates and thus have a chance to update the memcache.

However, with JDO, updates are achieved by updating attached objects and then letting the persistencemanager do the rest when it closes, and thus my code is never notified of updates. I could put events into all of my getters and setters to be notified of changes to my objects, but I would rather avoid that.

Any clues about how this is normally done would be appreciated.

+1  A: 

You can add a preUpdate hook to your JDO annotated objects. This sounds like it would work for your use case.

Example code from the link:

  @Entity
  public class Thing {
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     // Your Primary Key

     // Getters, setters, constructors, oh my!

     @PrePersist
     @PreUpdate
     public void prePersist() {
       // get JCache client instance
       // serialize object
       //store in cache
     }
} 

Edit: Oops, that's a JPA example. The link has JDO examples.

Ikai Lan
Actually I think that you would want to do it post-update. Given that the datastore likes to fail randomly, it would be good to first know that the data has been successfully persisted before putting it in the cache.
tempy