简体   繁体   English

提交中的mysql事务

[英]mysql Transaction in the commit block

CREATE DEFINER=`root`@`localhost` PROCEDURE `PrcCopyQuestion_Admin`(in Param1,in Param2 varchar(45))
BEGIN

 DECLARE EXIT HANDLER FOR SQLEXCEPTION
  BEGIN
      ROLLBACK;

Select 'Fail' as 'Status' ;      

  END; 

   DECLARE EXIT HANDLER FOR sqlwarning
  BEGIN
      ROLLBACK;

 Select 'Fail' as 'Status' ;         

  END; 

Start transaction;

   Insert statement 1; 

   Insert statement 2; 


   SELECT 'Success' AS 'Status'; 

call PrcGetQuestionAndOption_Admin(@variable); 

 Commit;

END

I am using Mysql 5.7 . 我正在使用Mysql 5.7 When in the commit block if the second (Insert statement 2) fails. 在提交块中时,如果第二个(插入语句2)失败。 It will go in the Rollback part and gives me output as 'Failed' . 它将进入回滚部分,并给我输出为'Failed' But when i am getting the output it still executes the Select 'Success' as Status in commit block. 但是,当我获得输出时,它仍会在提交块中执行'Success'状态'Success'选择'Success' So my question is when the second insert statement fails. 所以我的问题是第二条插入语句何时失败。 It should go directly in rollback and give me status as fail. 它应该直接回滚,并赋予我失败状态。 It should not execute the status as 'success' in commit block. 它不应在commit块中将状态执行为“成功”。

Eg: On rollback I am getting two result set: 例如:回滚时,我得到两个结果集:

Select 'Fail'..1st result set

Select 'Success'....2nd result set

I need output as only 我只需要输出

Select 'fail'

Any help appreciated!! 任何帮助表示赞赏!

I can't reproduce the problem. 我无法重现该问题。

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.12    |
+-----------+
1 row in set (0.00 sec)

mysql> DROP TABLE IF EXISTS  `t1`, `t2`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `t1` (
    ->   `c0` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `t2` (
    ->   `c0` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER //

mysql> DROP PROCEDURE IF EXISTS `PrcCopyQuestion_Admin`//
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE PROCEDURE `PrcCopyQuestion_Admin`(Param1 INT, Param2 VARCHAR(45))
    -> BEGIN
    ->   DECLARE EXIT HANDLER FOR SQLEXCEPTION
    ->     BEGIN
    ->       ROLLBACK;
    ->       SELECT 'Fail' Status;
    ->     END; 
    ->   DECLARE EXIT HANDLER FOR SQLWARNING
    ->     BEGIN
    ->       ROLLBACK;
    ->       SELECT 'Fail' Status;
    ->   END;
    ->   START TRANSACTION;
    ->   INSERT INTO `t1` (`c0`) VALUES (0);
    ->   IF Param1 = 0 THEN
    ->     INSERT INTO `ERR_t2` (`c0`) VALUES (0);
    ->   ELSE
    ->     INSERT INTO `t2` (`c0`) VALUES (0);
    ->   END IF;
    ->   SELECT 'Success' Status;
    ->   COMMIT;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> CALL `PrcCopyQuestion_Admin`(0, NULL);
+--------+
| Status |
+--------+
| Fail   |
+--------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> SELECT
    ->   `c0`
    -> FROM
    ->   `t1`;
Empty set (0.00 sec)

mysql> SELECT
    ->   `c0`
    -> FROM
    ->   `t2`;
Empty set (0.00 sec)

mysql> CALL `PrcCopyQuestion_Admin`(1, NULL);
+---------+
| Status  |
+---------+
| Success |
+---------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> SELECT
    ->   `c0`
    -> FROM
    ->   `t1`;
+----+
| c0 |
+----+
|  2 |
+----+
1 row in set (0.00 sec)

mysql> SELECT
    ->   `c0`
    -> FROM
    ->   `t2`;
+----+
| c0 |
+----+
|  1 |
+----+
1 row in set (0.00 sec)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM