子 mysql 事务中处理的错误是否会在调用它的 mysql 事务中触发回滚
Will errors handled in sub mysql transactions trigger rollbacks in the mysql transaction that calls it
假设我有一个名为 prodInner 的过程,它有一个像这样的错误处理程序
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1
@p1 = RETURNED_SQLSTATE, @p2 = MESSAGE_TEXT;
SELECT @p1 as RETURNED_SQLSTATE, @p2 as MESSAGE_TEXT;
ROLLBACK;
END;
和一个名为 prodOuter 的外部产品,具有相同的错误处理程序。假设当 运行 内部处理程序和 sqlexception 捕获它时出现问题。外部过程是否也会“失败”并回滚任何和所有更改(除了在其他过程调用中所做的更改之外)?
编辑
DROP PROCEDURE TEST_INNER;
CREATE PROCEDURE TEST_INNER()
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
START TRANSACTION;
INSERT INTO TEST_TABLE VALUES (1,0);
INSERT INTO TEST_TABLE VALUES(1); # throws an error because two values are required
COMMIT;
end;
CREATE PROCEDURE TEST_OUTER()
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
START TRANSACTION;
INSERT INTO TEST_TABLE VALUES (-1,0);
CALL TEST_INNER();
COMMIT;
end;
CALL TEST_OUTER()
CALL TEST_INNER()
编辑 2,this 是最佳解决方案吗?
我想我已经弄清楚了这个问题,但我的理解非常少。当调用外部过程时,将创建一个事务。然而,inner-procedure 中的提交将提交内部过程中的任何和所有更改,即使内部过程中的更改被回滚也是如此。
Statements That Cause an Implicit Commit
Transaction-control and locking statements. BEGIN, LOCK TABLES, SET autocommit = 1 (if the value is not already 1), START TRANSACTION, UNLOCK TABLES.
您可以详细调查一下:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=76bc26fa4e96b5ea3643aabe8161feea
假设我有一个名为 prodInner 的过程,它有一个像这样的错误处理程序
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1
@p1 = RETURNED_SQLSTATE, @p2 = MESSAGE_TEXT;
SELECT @p1 as RETURNED_SQLSTATE, @p2 as MESSAGE_TEXT;
ROLLBACK;
END;
和一个名为 prodOuter 的外部产品,具有相同的错误处理程序。假设当 运行 内部处理程序和 sqlexception 捕获它时出现问题。外部过程是否也会“失败”并回滚任何和所有更改(除了在其他过程调用中所做的更改之外)?
编辑
DROP PROCEDURE TEST_INNER;
CREATE PROCEDURE TEST_INNER()
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
START TRANSACTION;
INSERT INTO TEST_TABLE VALUES (1,0);
INSERT INTO TEST_TABLE VALUES(1); # throws an error because two values are required
COMMIT;
end;
CREATE PROCEDURE TEST_OUTER()
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
START TRANSACTION;
INSERT INTO TEST_TABLE VALUES (-1,0);
CALL TEST_INNER();
COMMIT;
end;
CALL TEST_OUTER()
CALL TEST_INNER()
编辑 2,this 是最佳解决方案吗?
我想我已经弄清楚了这个问题,但我的理解非常少。当调用外部过程时,将创建一个事务。然而,inner-procedure 中的提交将提交内部过程中的任何和所有更改,即使内部过程中的更改被回滚也是如此。
Statements That Cause an Implicit Commit
Transaction-control and locking statements. BEGIN, LOCK TABLES, SET autocommit = 1 (if the value is not already 1), START TRANSACTION, UNLOCK TABLES.
您可以详细调查一下:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=76bc26fa4e96b5ea3643aabe8161feea