Concurrency?
Hi, If multiple users register with same name at the same time, shouldn't this "select and insert" approach fail?
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.
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.
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.