I'm struggling with a bug that is closely related to this thread.  I think 
transactions all by themselves will not work here.  This is because the 
transactions are optimistic.  So, if the transaction looks like this (pseudo 
code)

pm.currentTransaction.begin();
Object preexistingEntity = pm.query("for secondary key");
if(null != preexistingEntity) throw SecondaryKeyException
pm.insert(new Entity("secondary key"));
pm.currentTransaction.commit().


The issue (I think) is that because transactions are optimistic, it is still 
possible for a race condition.  Two separate processes can create a 
transaction and attempt to read the preexistingEntity.  If they are very 
close in time (like my case, 32 milliseconds), then they will both return 
null, since the entity does not yet exist.  So, neither process will throw 
the SecondaryKeyException.  The will both go on and create new entitities 
and insert them.  Each of these entities will have different primary keys 
because the database creates these, but the entity's secondary key (an email 
address in this case) is not enforced by the database, so there is no error 
on either commit.  Thus, I end up with two entities with a duplicate value 
for a field that I want to be unique.  

I my case, this happened when the user double-clicked an activation link; 
 The server received two requests within 50 milliseconds of each other and 
both transactions completed without error.  

I want to use a human readable, unique field for my users names.  I need the 
database generated unique id to create relationships with other entities 
that survive even if the human readable field is changed.  For instance, if 
a user changes their email address, I don't want to have to go around to all 
the possible related entities and fixup references to it.  So, I use the 
primary Key for this.  But I still want the email address to be unique in 
the database.

Anyone from Google know how to make this happen?  HELP!!!!


-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" 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-java?hl=en.

Reply via email to