You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;
$$`
The text was updated successfully, but these errors were encountered:
存储过程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;
$$`
The text was updated successfully, but these errors were encountered: