处理MySQL异常处理程序访问异常 [英] MySQL exception handler access exception being handled
问题描述
我正在尝试回滚错误,但仍然让客户端收到错误。这可能实际上是不可能的,除非有一种方法来访问异常处理程序中的错误。
I'm trying to rollback on an error, but still let the client receive the error. This might actually be impossible, unless there is a way to access the error in an exception handler.
可以从异常抛出,即可以< a href =http://dev.mysql.com/doc/refman/5.5/en/signal.html =nofollow noreferrer>提出信号:
It's possible to "throw" from an exception, i.e. it's possible to raise a signal:
CREATE PROCEDURE p ()
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SIGNAL SQLSTATE VALUE '99999'
SET MESSAGE_TEXT = 'An error occurred';
END;
DROP TABLE no_such_table;
END;
但是这个MySQL文档的示例代码看起来很可怕,因为它从字面上将所有错误吞入其中。
But this sample code from the MySQL doc looks horrible, because it literally swallows all errors and jams them into one.
SHOW ERRORS
似乎是相关的,但我看不到任何办法它以编程方式,例如 SELECT代码FROM(SHOW ERRORS);
是不可能的。
这可能吗?有没有更好的做法,我完全失踪了?
Is this possible? Is there a better practice that I'm missing entirely?
推荐答案
看起来像 RESIGNAL
是您正在寻找的。 p>
Looks like RESIGNAL
is what you are looking for.
RESIGNAL
可以同时处理错误并返回错误信息。否则,通过在处理程序中执行SQL语句,导致处理程序激活的信息被破坏。RESIGNAL
也可以使一些程序更短,如果给定的处理程序可以处理部分情况,然后将条件up the line传递给另一个处理程序。
RESIGNAL
makes it possible to both handle an error and return the error information. Otherwise, by executing an SQL statement within the handler, information that caused the handler's activation is destroyed.RESIGNAL
also can make some procedures shorter if a given handler can handle part of a situation, then pass the condition "up the line" to another handler.
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`resig` $$
CREATE PROCEDURE `test`.`resig` ()
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SELECT 'I executed something before throwing the error' as `this_works`;
RESIGNAL;
END;
SELECT foo FROM bar WHERE baz = 0;
END $$
DELIMITER ;
mysql> call resig();
+------------------------------------------------+
| this_works |
+------------------------------------------------+
| I executed something before throwing the error |
+------------------------------------------------+
1 row in set (0.00 sec)
ERROR 1054 (42S22): Unknown column 'foo' in 'field list'
mysql>
这篇关于处理MySQL异常处理程序访问异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!