Hello everybody,
This situation has to have a standard solution but I couldn't find it so far.
Imagine multimple users create their accounts and there is a table to store the personal data one of the fields of which is to keep the user nickname. Nicknames have to be unique.
Here is a simplified procedure to create a new user account:
Before inserting a record with a new user the procedure checks whether such a nickname exists in the table (calls check_nick).
Now, if two new users create accounts at the same time and choose the same nickname there will be a problem.
I've read about transactions, locking tables etc. but so far I don't have a clear understanding which tool to use. For example, they say locking a table doesn't prevent the other users from querying it. So suppose, the first user's procedure calls check_nick and gets FALSE (no such nickname in the table) and before it commits the insertion of a new row the second user's procedure calls in turn the check_nick and also gets FALSE (am I right here?) and, therefore will attempt to insert another record with the same nickname (of course, there is UNIQUE constraint on the vc_user_nickname field).
How should I change the procedure to prevent this kind of trouble?
Thank you,
Alex
This situation has to have a standard solution but I couldn't find it so far.
Imagine multimple users create their accounts and there is a table to store the personal data one of the fields of which is to keep the user nickname. Nicknames have to be unique.
Here is a simplified procedure to create a new user account:
Code:
PROCEDURE crt_account(vc_in_nick IN VARCHAR2,
vc_in_pw IN VARCHAR2,
i_out_error OUT INTEGER)
AS
bl_nick_exists BOOLEAN;
BEGIN
check_nick(vc_in_nick,bl_nick_exists);
IF bl_nick_exists = FALSE THEN
INSERT INTO tbl_Acccounts
(
vc_user_nickname,
vc_user_pw
)
VALUES
(
vc_in_nick,
vc_in_pw
);
COMMIT;
i_out_error := 0;
ELSE
RAISE exc_nickname_taken;
END IF;
EXCEPTION
WHEN exc_nickname_taken THEN
i_out_error := -1;
END crt_account;
/
Before inserting a record with a new user the procedure checks whether such a nickname exists in the table (calls check_nick).
Now, if two new users create accounts at the same time and choose the same nickname there will be a problem.
I've read about transactions, locking tables etc. but so far I don't have a clear understanding which tool to use. For example, they say locking a table doesn't prevent the other users from querying it. So suppose, the first user's procedure calls check_nick and gets FALSE (no such nickname in the table) and before it commits the insertion of a new row the second user's procedure calls in turn the check_nick and also gets FALSE (am I right here?) and, therefore will attempt to insert another record with the same nickname (of course, there is UNIQUE constraint on the vc_user_nickname field).
How should I change the procedure to prevent this kind of trouble?
Thank you,
Alex