Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrency? #10

Open
qding-bot opened this issue Sep 17, 2018 · 3 comments
Open

Concurrency? #10

qding-bot opened this issue Sep 17, 2018 · 3 comments

Comments

@qding-bot
Copy link

Hi, If multiple users register with same name at the same time, shouldn't this "select and insert" approach fail?

@eh3rrera
Copy link
Owner

Hi. I'm not sure what you mean, this part of the code checks that case:

User existingUser = service.getUserbyUsername(user.getUsername());
if(existingUser == null) {
    service.registerUser(user);
    res.redirect("/login?r=1");
    halt();
} else {
    error = "The username is already taken";
}

Are you saying this is not working?

Well, It's possible due to concurrency, isolation level of the db, etc.

@qding-bot
Copy link
Author

Yeah. I mean, what if:

Thread1: user1 -> getUserbyUsername('jimmy') -> none found -> registerUser('jimmy')
Thread2: user2 -> getUserByUsername('jimmy') -> none found -> registerUser('jimmy')

Then two jimmy users are created.

@eh3rrera
Copy link
Owner

Right, it's not likely, but it can happen.

Assuming the application is using a single database (or a cluster that can be seen as a single database), the easiest way to solve this could be at the database level, adding a unique constraint to the username column, so the second insertion fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants