views:

98

answers:

3

Given:

mysql_query("START TRANSACTION");
mysql_query("UPDATE foo = 'bar'");
die();

die() stops the transaction before I can COMMIT or ROLLBACK. Looking at my tables, it appears that the update doesn't take place, but if I run queries later in another script, are they still part of that initial transaction? Is the transaction still 'open' until I COMMIT or ROLLBACK?

+2  A: 

If die kills the connection to mysql then yes, the transaction is closed. If you're just leaving the transaction hanging though you're going to get in trouble.

I would recommend making sure that the transaction and the commit are in the same mysql_query if at all humanly possible to ensure that it actually happens.

Chuck Vose
According to http://www.php.net/manual/en/function.mysql-close.php:"Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution."Since die() ends the script's execution, I would assume that means that the database connection is closed?
Matthew
And unfortunately, I'm not using mysqli, so I can't do multiple queries at once.
Matthew
That's correct, I was just wondering if you were really going to be completely faithful about always die()ing after your transaction was opened. I mean, how often are you really going to die() in a real-life scenario? That's why I mentioned that you probably shouldn't rely on die() to close a transaction for you.
Chuck Vose
A: 

Only if you continue working with the same resource ID. Otherwise it will be considered a new transaction. If the resource ID for the unfinished transaction is not used again it essentially is discarded.

John Conde
A: 

It depends on your serialization level. If a transaction is neither committed or rolled back, it should time out after a while and rolled back in the DB. But until then it is an unfinished transaction which might cause inconsistent selects in other non serializable transactions. Dependent on it's level:

  • read uncommitted: you can read the uncommitted, unfinished values until rollback
  • repeatable read (mysql default): you won't read uncommitted values, but as ranges are not locked, a SELECT COUNT(*) FROM customers WHERE 18 < age AND age < 24 is not guaranteed to return consistent values until rollback
sibidiba