Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql transaction rollback on failure in update

With a simple transaction as

START TRANSACTION;
UPDATE posts SET status='approved' where post_id='id' AND status != 'approved';
.. other queries ...
COMMIT;

I want to perform the transaction only once when changing the status; but the above UPDATE will not give an error to rollback the transaction when no row is updated.

How can I limit the transaction to commit only if the row is updated (I mean the status is changed).

like image 876
Googlebot Avatar asked Feb 17 '12 11:02

Googlebot


People also ask

Can we rollback update statement in MySQL?

To have possibility to ROLLBACK DML statements (like INSERT , UPDATE or DELETE queries) you should use transaction blocks: START TRANSACTION; UPDATE CUSTOMERS SET ADDRESS = 'Pune' WHERE ID = 6; -- and more DML queries COMMIT; -- or ROLLBACK; Since transaction was COMMIT ed it can not be rolled back.

Why rollback is not working in MySQL?

This means that each individual SQL statement is treated as a transaction and is automatically committed right after it is executed. So if you need to do transactions yourself, you must turn off the autocommit mode by AUTOCOMMIT = 0 .

Does SQL transaction rollback on error?

By setting XACT_ABORT to ON and we can rollback all the statements inside a transaction when an error occurred. Thus, let's rewrite the code again in this manner. It will also roll back the transaction when the error occurred in the third statement.

Can a transaction rollback fail?

If a rollback fails, then you would have a serious problem. The reliability of the database cannot be guaranteed. In other words; you probably have some sort of corruption in your transaction log and will end up with an inconsistent database.

How do I rollback a transaction in MySQL?

To roll back the current transaction and cancel its changes, you use the ROLLBACK statement. To disable or enable the auto-commit mode for the current transaction, you use the SET autocommit statement. By default, MySQL automatically commits the changes permanently to the database.

What is a rollback SQL statement?

The rollback SQL statement is used to manually rollback transactions in MS SQL Server. Transactions in SQL Server are used to execute a set of SQL statements in a group.

What is rollback and set autocommit in MySQL?

ROLLBACK rolls back the current transaction, canceling its changes. SET autocommit disables or enables the default autocommit mode for the current session. By default, MySQL runs with autocommit mode enabled. This means that, when not otherwise inside a transaction, each statement is atomic, as if it were surrounded by START TRANSACTION and COMMIT.

How do I control transactions in MySQL?

MySQL provides us with the following important statement to control transactions: To start a transaction, you use the START TRANSACTION statement. To commit the current transaction and make its changes permanent, you use the COMMIT statement. To roll back the current transaction and cancel its changes, you use the ROLLBACK statement.


1 Answers

Here is in PHP (haven't tested, needs adapting to your situation):

mysql_query('START TRANSACTION;')
mysql_query("UPDATE posts SET status='approved' where post_id='id' AND status != 'approved';");
if (mysql_affected_rows()){
    mysql_query('COMMIT');
} else {
    mysql_query('ROLLBACK');
}

Or, If you want to be clever and do it in SQL (using ROW_COUNT() and IF):

START TRANSACTION;
UPDATE posts SET status='approved' where post_id='id' AND status != 'approved';
SELECT ROW_COUNT() INTO @affected_rows;
-- .. other queries ...
IF (affected_rows > 0) THEN
    COMMIT;
ELSE
    ROLLBACK;
END IF
like image 98
Vyktor Avatar answered Oct 20 '22 21:10

Vyktor