Home > Back-end > High concurrency, prevent user name repeated injection problem, for SpringMVC MySQL
High concurrency, prevent user name repeated injection problem, for SpringMVC MySQL
Time:10-10
Have a users table, fields are respectively, id, username, password, and record the user's id (primary key), the user name, and password,
my business logic for the user name field, you must not have repeat, or the whole system is completely confused,
Way is before me, in the control layer, before inserting new users, to judge the first user name, if it exists already? If you already exists, an error prompt to the user, if not, then insert operation,
Pseudo code is as follows:
Transaction start If (xxxDao. IsUsernameExist (" love ")) { Return "the user name you entered has been occupied, please try other user name, thank you!" ; /* * use SQL statements to: Select * from the user where the username='love' */ } else { XxxDao. InsertUser (user); Return "user registration, please login to return to the login page." ; /* * use SQL statements to: INSERT INTO user (id, username, password) VALUES (' XXXX ', 'love', 'XXXXXXX) */ } End of the transaction
I test at the beginning, this program is running very perfect, what also have no! But later, problem, ruthless come,,,
When you have more than one person, such as 100 people, concurrent access, are problems:
The 100 comrade, registered the user name "love" at the same time, so they first perform the if (xxxDao. IsUsernameExist (" love ")) this statement, to judge the user list, whether there is love this username, so their results at the same time, there is no, you can register the user name, and then began to perform the INSERT INTO... Operation, the result is my user list, there were 100 user name is a record of your love,,,
It is finished,,
So I want to ask everybody a great god, if I put the code to do a little modification, modification as follows:
Transaction start If (xxxDao. IsUsernameExist (" love ")) { Return "the user name you entered has been occupied, please try other user name, thank you!" ; /* * use SQL statements to: Select * from the user where the username='love' */ } else { XxxDao. InsertUser (user); Return "user registration, please login to return to the login page." ; /* * use SQL statements to: INSERT INTO user (id, username, password) SELECT 'XXXX', 'love', 'XXXXXXX FROM DUAL WHERE NOT the EXISTS (SELECT * FROM the user WHERE the username=' love ') */ } End of the transaction
If so, will there are more than the concurrency issues? Great god guide me, as far as possible can not lock won't go with lock, and can not go to modify the transaction isolation level, just try not to modify, unless there is no way
CodePudding user response:
To achieve concurrency is not a problem, must do ion level, only logic code cannot prevent concurrent, so must use lock, This lock has a skill, "dynamic" lock, lock the username to use this variable to do synchronized (username. Intern ()) {XXXXX}, and after the username intern () method calls, so you can put the same user name concurrent queue, as is not the same without queuing, (add on query judgment)