Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9324241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:32:55+00:00 2026-06-19T04:32:55+00:00

I know this is a classical problem, but I still don’t know how to

  • 0

I know this is a classical problem, but I still don’t know how to do it. On Google App Engine, I have a member registration form which uses jQuery’s validation to check if a username exists.
There of course is a concurrency problem: several users try to register, enter the same username, Validation finds the username available, and allow them to press “Add” at the approximately same time. Validation wouldn’t detect this. In my application, username, email, and Personal ID should all be unique. How do I prevent the following code from having the concurrency problem:

member = Member()
member.username = self.request.get('username')
member.Pid = self.request.get('Pid')
member.email = self.request.get('email')
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T04:32:56+00:00Added an answer on June 19, 2026 at 4:32 am

    As the uniqueness constraint is on username, you have to use it as key in datastore and use transactions.

    def txn():
        key = ndb.Key(Member, username)
        member = key.get()
        if member is not None:
            raise CustomAlreadyExistsException(member)  # This will abort txn
        member = Member(
            id=username,
            Pid=self.request.get('Pid'),
            email=self.request.get('email'),
            ...)
        member.put()
    ndb.transaction(txn)
    

    This makes sure only one person can register a username.

    The jQuery helper would check if ndb.Key(Member, userid).get() gives a result or not. The GET is not transactional.

    To improve usability client side in “reserving” a username after checking availability, you could use memcached as suggested by Daniel, but I’d call YAGNI, skip the complexity and rather let some people get validation error after submitting the form. Note that memcached is best effort and has no guarantees about anything.

    If you need guaranteed uniqueness on multiple fields, you have to add Model classes for them and check in a cross group (XG) transaction.

    class Pid(ndb.Model):
        member = ndb.KeyProperty()
    
    class Email(ndb.Model):
        member = ndb.KeyProperty()
    
    class Member(ndb.Model):
        pid = ndb.KeyProperty()
        email = ndb.KeyProperty()
    
        @property
        def pid_value(self):
            return self.pid.id()
    
        @property
        def email_value(self):
            return self.email.id()
    
    def txn():
        member_key = ndb.Key(Member, username)
        pid_key = ndb.Key(PersonalId, self.request.get('Pid'))
        email_key = ndb.Key(Email, self.request.get('email'))
    
        member, pid, email = ndb.get_multi([member_key, pid_key, email_key])
    
        if member is not None or pid is not None or email is not None:
            raise CustomAlreadyExistsException(member, pid, email)  # This will abort txn
    
        # Create instances referencing each other
        email = Email(key=email_key, member=member_key)
        pid = Pid(key=pid_key, member=member_key)
        member = Member(
            key=member_key,
            pid=pid_key,
            email=email_key,
            ...)
        ndb.put_multi([member, pid, email])
    
    ndb.transaction(txn, xg=True)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is a recurrent/classical topic but I did not found anything that
I know this is an old question, but I have spend any hours on
I know that are innumerous posts out there with this problem and solutions, but
Know this might be rather basic, but I been trying to figure out how
i know this is a stupid question but i d'ont know how to do
I know this is possible in Perl, but I was wondering if this can
I know this is probably something simple but I can't seem to find anything
I know this is repeated question on Stack. But in my case the URL
I know this is an easy concept, but I just can't seem to get
First off, I know this has been discussed over and over again . But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.