Women in Technology

Hear us Roar



Article:
  Best Practices for Exception Handling
Subject:   "Alternatively, you can even provide a method that checks if the username is already taken." not working
Date:   2003-11-24 00:31:32
From:   anonymous2
"Alternatively, you can even provide a method that checks if the username is already taken."
does not work!


for example, I have a
register(User) method, who's singnature is like


public register(User) throws DuplicateUsernameException


if I provide a method that checks if the username is already taken. does that mean , I can change the singnature like
public register(User);
// remove the throws DuplicateUsernameException
that is totally wrong, because the author ignore the multi-client case, that is ,
thread 1: checkUserNameExist("myname"); //true
thread 2: checkUserNameExist("myname"); //true
thread 1: register("myname"); //success
thread 2: register("myname"); //should fail, but the caller does not know.




Main Topics Oldest First

Showing messages 1 through 1 of 1.

  • "Alternatively, you can even provide a method that checks if the username is already taken." not working
    2003-12-03 14:07:00  anonymous2 [View]

    I think the author implicitly assumes you would write thread-safe code. Whether or not a method would be made of checking if the username exists, instead of throwing an exception, it still has to be checked.

    By writing a thread-safe register method that would synchronize the checking for the username and the registering in one block, it should work.