Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to write ROLLBACK if queries fail?

I write

mysql_query("SET AUTOCOMMIT=0");
mysql_query("START TRANSACTION");

before I write all queries. Then check if all of them are true and then write:

mysql_query("COMMIT");

But if one of query fails, I just pass COMMIT query. So do I really need ROLLBACK function if one of the queries fail? Because without ROLLBACK it also works.
Thanks.

like image 699
good_evening Avatar asked May 01 '10 18:05

good_evening


People also ask

When should rollback be used?

Rollback is used to maintain the integrity of the database. So it is either full in or none. For e.g suppose you have 10 rows that you are supposed to insert into a table and on the 8th row there is an issue , then under such a case all the transactions will be rolled back.

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.

Why do we need rollback?

In database technologies, a rollback is an operation which returns the database to some previous state. Rollbacks are important for database integrity, because they mean that the database can be restored to a clean copy even after erroneous operations are performed.

Do we need to commit after rollback?

I have confirmed that after rollback we cannot commit the same transaction. Make sure another transaction is not in waiting, else it will be committed. Also don't do anything with transaction in finally block. Always use finally block to close connection , data reader etc.


2 Answers

The reason to use transactions is to group multiple changes together so they all succeed atomically, or else if they can't, don't do any of them. In other words, if any change fails, the transaction would leave the database in a logically inconsistent state.

Example: debit one account in one UPDATE, and credit a different account in a separate UPDATE. This represents a money transfer. If the debit succeeds but the credit fails, you should roll back the whole transaction or else it appears that money vanished into thin air.

So the intended usage would be to roll back the transaction if one of the changes fails.

You seem to be saying that in your application, it's okay if one of the changes fails. This makes me think that you've grouped changes into transactions inappropriately.

Decide which group of changes must all succeed together, and put those into one transaction. Any changes that don't go with this group should be in a separate transaction.

like image 87
Bill Karwin Avatar answered Nov 09 '22 08:11

Bill Karwin


I think you're asking if executing ROLLBACK is necessary, since without it the commits still don't get applied. That's technically true, but only because the transaction is still open since you haven't ended it. Anything that implicitly commits the transaction (for example, starting a new transaction) will act as though you ran COMMIT, which is the opposite of what you want

like image 42
Michael Mrozek Avatar answered Nov 09 '22 07:11

Michael Mrozek