mysql errors and transaction rollback
Posted 03 August 2006 - 07:11 PM
I have to insert multiple lines of user details into 2 different tables.
I currently have a script that loops through the lines and adds accordingly.
However if there is an error I need to undo all the entries I have made.
Could somebody tell me will transaction rollback help me in this case?
I currently have the following which is not working where the transaction code is out side the loop.
I'm assuming this is wrong but I'm trying to find a way to avoid having to step back through 50 entries for example if the error is produced in the last line
code follows - thanks for any help or info....
//do some SQL queries
if there is an error increment $how_many_errors
Posted 03 August 2006 - 07:23 PM
Posted 03 August 2006 - 07:26 PM
"Do not use transactions (e.g. with InnoDB MySQL tables) with persistent connections. If your script stops or exits for any reason, your transaction will be left open and your locks will be left on. You have to reset MySQL to release them (so far as I can figure). They won't ROLLBACK automatically on error, like they ought to. When you restart the script, you'll get a new connection, so you can't rollback or commit for the previous script. Any script with a start transaction, rollback, or commit SQL statement should use regular (not persistent) connections. Seems like PHP ought to automatically issue a ROLLBACK on any open transactions when a script exits (error or otherwise) without a COMMIT. ZEND's site has a brief blurb on this. It's OK to mix/match so you use a persistent connection for the read stuff, but open a new regular connection conditionally (if you have to update, which is usually less often)."
Andy also this link if you havn't seen it yet http://dev.mysql.com.../en/commit.html
Posted 03 August 2006 - 07:51 PM
shocker-z Thanks for the info - I had a quick read and tried setting autocommit to 0 but it's still not rolling back. I'm finding it pretty confusing to figure out exactly what you can and can't do with transactions. Where you quoted "Do not use transactions (e.g. with InnoDB MySQL tables) with persistent connections." does that mean I have to reconnect for each query? if so it kind of defeats the object of me trying to use rollback. Sorry if these are dumb questions, I'm no master of this stuff!
Posted 03 August 2006 - 09:02 PM
As far as your question is concerned. If you're saying you simply want to undo all that you've done if everything didn't go according to plan, then rolling back the transaction will be fine. You should make your table types "INNODB" if you'd like to do that.
Posted 03 August 2006 - 09:06 PM
((EMAIL CODE THAT WORKS))
((PAYPAL INTEGRATION THAT WORKS))
Posted 03 August 2006 - 11:39 PM
I don't fully understand what you mean but the main thing to remember is that depending on the degree to which you change the database it becomes very difficult to manually keep track yourself.
shoz wouldnt it work if the user set a session with the old entries then use a simple update page
In addition, depending on the error that occurs while making the changes it can become even more complicated. If it's critical that multiple entries/changes occur to the database without error, use the INNODB engine and Transactions.
Posted 04 August 2006 - 12:07 PM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users