views:

46

answers:

3

My code:

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

insert_query, update_query1, update_query2

mysql_query("COMMIT");
update_query3

Why does update_query3 query doesn't work if I put it after COMMIT? It works if I put it before the COMMIT. Why is that? It's really strange.

Thank you

A: 

Because COMMIT (or ROLLBACK for that matter) marks the end of the transaction.

You'd have to use:

mysql_query("COMMIT AND CHAIN");

..to create a new transaction to begin as soon as the current one ends, and the new transaction has the same isolation level as the just-terminated transaction.

But it still means you need to have:

mysql_query("COMMIT");

...after the update_query3 to commit the changes.

OMG Ponies
+1  A: 

First you disable auto-commit. Then you begin transaction, make changes and commit them. Then you execute another query that implicitly starts another transaction. In order for changes to get committed, you have to call "COMMIT" explicitly because auto-commit is turned off.

Vlad Lazarenko
A: 

update_query3 isn't part of any existing transaction. So it starts a new (implicit) transaction.

You never commit the transaction started by update_query3, so it'll get rolled back when you close the connection.

nos
Because autocommit was disabled, there are no implicit transactions.
OMG Ponies
Well, it looks like mysql starts a new transaction for you if you don't issue `begin` yourself, with autocommit=0;
nos
BEGIN and START TRANSACTION are interchangeable; I wonder if the issue is really that the tables are MyISAM (which doesn't support transactions anyway).
OMG Ponies