Unable to get saved entity using objectify

2019-06-09 06:44发布

I am unable to get my saved entity reliably using Objectify.

It looks like the cache is getting corrupted. The strange thing is - I can see the saved entity correctly though admin console datastore viewer. Also I wrote a small program to view the entity using RemoteApi and I can see the saved value correctly.

When I query the entity successively using a servlet or a cloud endpoint rest api - my successive queries are giving different results, and it looks like something in the datastore/cache is getting corrupted.

My entity looks like this.

class ContentEntity {
  @Id Long id;
  String html;
  @Index String tag;
  boolean publish;
}

I save it like this.

ContentEntity entity = ofy().load.type(ContentEntity.class)
    .filter("tag", "my tag").first().get();

if (null == entity)
    entity = new ContentEntity();

entity.html = "my html";
entity.tag = "my tag";
entity.publish = true;

ofy().save.entity(entity).now();

I retreive it like this.

ContentEntity entity = ofy().load().type(ContentEntity.class).
            filter("tag", "my tag").first().get();

What happens is as follows -

1) Let the intial value of ContentEntity.html be "value 1"
2) save a new value - "value 2"
3) using admin console datastore viewer I can see "value 2" is saved correctly. (using remote api also I can see "value 2")
3) View the entity through servlet or rest api using the retrieve code pasted above. I see "value 2"
4) View the entity again through servlet or rest api. I see "value 1"
5) View it again. I see "value 2"
it keeps switching between "value 1" and "value 2"

It all worked fine in my dev environment but not in appengine.

Looks like I am doing something wrong and not handling eventual consistency correctly. I always want strongly consistent results. I don't mind if my queries are little slower. What should I do ?

Any tips/suggestions/help would be much appreciated.

Regards,

Sathya

1条回答
劫难
2楼-- · 2019-06-09 06:48

It turns out this is because I had forgotten to add objectify filter in web.xml as mentioned in Objectify wiki page

I added the following in my web.xml and the problem was solved.

<filter>
    <filter-name>ObjectifyFilter</filter-name>
    <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ObjectifyFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Regards,

Sathya

查看更多
登录 后发表回答