If you want it to be unique you'll need to use the keyname, like your earlier 
idea. Fetch by key, if it doesn't exist you're ok to create it. 


Robert




On Aug 24, 2011, at 0:12, Mike Gleason jr Couturier <[email protected]> 
wrote:

> And let's say I do it on a property, is this would be enough?
> 
>            Query q = new Query("User').setKeysOnly();
>            q.addFilter("Email", Query.FilterOperator.EQUAL, "[email protected]");
> 
>            Transaction tx = datastore.beginTransaction();
> 
>            if
> (datastore.prepare(q).countEntities(FetchOptions.Builder.withDefaults())
>> 0)
>            {
>                tx.rollback();
>                return false;
>            }
> 
>            datastore.put(entity);
>            tx.commit();
>            return true;
> 
> 
> On Aug 23, 11:23 pm, Mike Gleason jr Couturier
> <[email protected]> wrote:
>> 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.
> 

-- 
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.

Reply via email to