简体   繁体   English

什么是正确的方法来处理异常并在错误时回滚mysql中的事务?

[英]What is correct way to handle exception and rollback a transaction in mysql on error?

Background : 背景 :

I'm just a beginner in MYSQL. 我只是MYSQL的初学者。 And trying to do transaction operations in MYSQL. 并尝试在MYSQL中执行事务操作。

Query: 查询:

I am trying to alter table in mysql, I am just changing data type of a column. 我试图更改mysql中的表,我只是更改列的数据类型。 To do this operation, using a tansaction. 要执行此操作,请使用tansaction。 What is the good way to rollback a transaction on error ? 发生错误时回滚事务的好方法是什么?

SQL Sever Example to Handle The Exception: SQL Sever处理异常的示例:

USE XYDB

 BEGIN TRY

 BEGIN TRANSACTION;

    ALTER TABLE <table> ALTER COLUMN <column> MEDIUMTEXT;
 COMMIT TRANSACTION;  

END TRY 

BEGIN CATCH  

IF @@TRANCOUNT > 0  

    ROLLBACK TRANSACTION;  

    SELECT   

    ERROR_NUMBER() AS ErrorNumber  

,ERROR_SEVERITY() AS ErrorSeverity  

    ,ERROR_STATE() AS ErrorState  

    ,ERROR_PROCEDURE() AS ErrorProcedure  

    ,ERROR_LINE() AS ErrorLine  

    ,ERROR_MESSAGE() AS ErrorMessage;  

    END CATCH;  
GO  

Alter Table is a DDL command, you can use same in MySQL as below: Alter Table是DDL命令,您可以在MySQL中使用以下命令:

ALTER TABLE <table> CHANGE COLUMN <column> Parchar(100);

The above query is auto commit , you can never rollback the same. 上面的查询是auto commit,您永远无法回滚。

If you want to rollback ,again run alter table command with desired data type of column. 如果要回滚,再次使用所需的列数据类型运行alter table命令。

It is not possible to use DDL (create/alter table) statements in a transaction in MySQL. 在MySQL的事务中不能使用DDL(创建/更改表)语句。

These statements have their own "transaction" (separate for each statement), which is committed automatically. 这些语句具有自己的“事务”(每个语句分别),该事务将自动提交。

If you run an ALTER TABLE ... in a transaction, the transaction will be committed before the alter. 如果在事务中运行ALTER TABLE ... ,则该事务将在更改之前提交。

If it is only one statement, there is no need to do anything, it will either fail (and the table will remain unchanged), or succeed (and the table will be altered correctly). 如果仅是一条语句,则无需执行任何操作,它要么失败(表将保持不变),要么成功(表将被正确更改)。

If you need to do multiple ALTER .. 's, there is no way to group them into 1 transaction. 如果您需要执行多个ALTER .. ,则无法将它们分组为1个事务。

The previous commenters answered you about whats wrong with your posted code. 先前的评论者回答了您发布的代码有什么问题。 About the exception handling procedure itself: here is the example of rollback code block with ability to log the error to some other table without loosing the exception details in MySQL and throwing the error again after logging it. 关于异常处理过程本身:这是回滚代码块的示例,能够将错误记录到其他表中,而不会丢失MySQL中的异常详细信息,并且在记录错误后再次抛出错误。

# CREATE PROCEDURE AND OTHER DECLARE STATEMENTS HERE
# ....

DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
    GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE, @errno = MYSQL_ERRNO, @text = MESSAGE_TEXT;

    ROLLBACK;

    SET @full_error = CONCAT('ERR:', @errno, '(', @sqlstate, '):', @text);

    CALL sp_logaction(@full_error); # Some logging procedure

    RESIGNAL;
END;
# PROCEDURE BODY WITH START TRANSACTION & COMMIT HERE
# .....

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

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