I've done this, I just wan to have a second opinion:
Entity entity = new Entity("User", "mike" /*username*/);
Transaction tx = datastore.beginTransaction();
try {
datastore.get(entity.getKey());
return false; // error, already in hrd
} catch (EntityNotFoundException e) {
datastore.put(entity);
tx.commit();
return true;
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
Seems a bit overkill but we need consistency right? :)
Thanks
On Aug 23, 10:39 pm, Mike Gleason jr Couturier
<[email protected]> wrote:
> Hi,
>
> Given the nature of the "put" function (low level hrd API), how can I
> atomically put a new entity without overwriting an existing one if the
> keys are chosen by the users (String values)?
>
> Let's say I have 2 simultaneous users that creates at the same time an
> entity of kind "User" with the custom key "johndoe" (because the user
> name is the key), I don't want the second one to overwrite the
> first...
>
> I don't want to have a property for the username since I'll have the
> same "unique index" problem.
>
> Is a transaction with a:
> 1) get
> 2) catch EntityNotFoundException
> 3) put
> 4) commit
>
> enough to ensure consistency ?
>
> Thanks
--
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en.