Hi guys,
Thank you for your answers.
I do not think I have explained my problem properly though when I read your
answers.
I have a table "order" where I save different information about an order.
The unique id of the order comes from an external system, and I want to
save this unique id for the order.
When an order is updated in the external system, I want to update the GAE
datastore accordingly.
For this reason I first check whether the order that was updated in the
external system already exists in my datastore. If it already exists I
update it, if it does not already exist, I create the order in the
datastore.
My problem is that I use this:
datastore.prepare(query).asSingleEntity();
which fails, as there are more than one entity with the unique id.
I do not understand how several entities can be created with the same
unique id, when my logic says:
Entity entity = getEntity(uniqueId);
if (entity == null){
createEntity();
}else{
updateEntity();
}
It is as if the getEntity check does not detect that there already is an
entity with the unique id.
It does seem that this is caused by a timing/concurrency issue - the
external system sends two updates approximately at the same time, but
shouldn't the datastore be able to handle this?
My first attempt of getEntity was:
Query query = new Query(OrderDBFields.ORDER_TABLE_NAME).setFilter(new
Query.FilterPredicate(OrderDBFields.ID, Query.FilterOperator.EQUAL, id));
return datastore.prepare(query).asSingleEntity();
My second (confused attempt) is:
Transaction txn = datastore.beginTransaction();
Key orderKey = KeyFactory.createKey(OrderDBFields.ORDER_TABLE_NAME,
id);
Entity orderEntity = null;
try {
orderEntity = datastore.get(orderKey);
} catch (EntityNotFoundException e) {
orderEntity = null;
}
txn.commit();
return orderEntity;
Any input is appreciated,
Thanks
-Louise
Den onsdag den 27. januar 2016 kl. 12.39.54 UTC+1 skrev Louise Elmose
Hedegaard:
>
>
> Hi,
>
> I have an issue with too many entities being created in my GAE datastore.
>
> I have an unique id on an entity (which is not the key in this case), and
> there should thus only be one entity in the datastore per unique id.
> When an entity is updated or created by the user, I check whether the
> entity already exists in the DB, and only create a new entity, if there is
> not already an existing entity with a matching id. If there is an existing
> entity, I update the parameters of the entity.
> This logic should ensure that there is only one entry/entity per unique
> id, but somehow several entities with the same id are created in the DB - I
> do not understand how/why!
>
> When I look at this page
> https://cloud.google.com/appengine/docs/java/datastore/entities#Java_Updating_an_entity
>
> is says "You can use a transaction
> <https://cloud.google.com/appengine/docs/java/datastore/transactions> to
> test whether an entity with a given key exists before creating one.".
>
> Do I really need to use transactions to validate whether there is an
> existing entity with a given id, or does that only apply when you use the
> key as primary key??
>
> Thanks,
> -Louise
>
--
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-appengine/c774c62a-98d8-467d-b97a-c5e817644cc5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.