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

存储过程seckill.sql有点问题 #48

Open
xiaogeniubi opened this issue May 10, 2021 · 0 comments
Open

存储过程seckill.sql有点问题 #48

xiaogeniubi opened this issue May 10, 2021 · 0 comments

Comments

@xiaogeniubi
Copy link

存储过程seckill.sql有点问题,导致重复秒杀时仍然秒杀成功,改成下面就ok了。
`DELIMITER $$ -- console中 ; 转换为 $$

CREATE PROCEDURE seckill.execute_seckill
(IN v_seckill_id BIGINT, IN v_phone BIGINT,
IN v_kill_time TIMESTAMP, OUT r_result int)
BEGIN
DECLARE insert_count INT DEFAULT 0;
START TRANSACTION ;
INSERT IGNORE INTO success_killed
(seckill_id, user_phone,create_time)
VALUES (v_seckill_id, v_phone,v_kill_time);
SELECT row_count() INTO insert_count;
IF (insert_count = 0) THEN
ROLLBACK ;
SET r_result = -1;
ELSEIF (insert_count < 0) THEN
ROLLBACK ;
SET r_result = -2;
ELSE
UPDATE seckill
SET number = number -1
WHERE seckill_id = v_seckill_id
AND end_time > v_kill_time
AND start_time < v_kill_time
AND number > 0;
SELECT row_count() into insert_count;
IF (insert_count = 0) THEN
ROLLBACK;
SET r_result = 0;
ELSEIF (insert_count < 0) THEN
ROLLBACK;
SET r_result = -2;
ELSE
COMMIT;
SET r_result = 1;
END IF;
END IF;
END;
$$`

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

1 participant