Jump to content

Archived

This topic is now archived and is closed to further replies.

langley

mysql errors and transaction rollback

Recommended Posts

Hi,

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....

$how_many_errors=0;

mysql_query('BEGIN TRANSACTION_NAME');

for($b=0;$b<$blah;$b++){
//do some SQL  queries
if there is an error increment $how_many_errors
}

if($how_many_errors>0) {
mysql_query('ROLLBACK');
}
else {mysql_query('COMMIT');}

Share this post


Link to post
Share on other sites
You should treat the error right way instead of doing a rollback.

Share this post


Link to post
Share on other sites
From what i've read this should work for you.. her's some info i found from searching around.

"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 [url=http://dev.mysql.com/doc/refman/5.0/en/commit.html]http://dev.mysql.com/doc/refman/5.0/en/commit.html[/url]


Regards
Liam

Share this post


Link to post
Share on other sites
[b]CTM[/b]  - I realise that but if for whatever reason there is some kind of failiure to complete inserting of ALL users I need to take out any that may have been inserted by the loop thusfar and I'm trying to avoid going back and deleting each set of details from both tables 1 by 1

[b]shocker-z[/b] 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!

Share this post


Link to post
Share on other sites
The persistent connection comment has to do with the fact that PHP will reuse the connection. Meaning any open transaction will still be open for the next script that uses the connection. The short answer for that is to use [url=http://www.php.net/mysql_connect]mysql_connect()[/url] instead of [url=http://www.php.net/mysql_pconnect]mysql_pconnect()[/url].

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.

http://www.innodb.com/ibman.php
http://dev.mysql.com/doc/refman/5.0/en/innodb.html

Share this post


Link to post
Share on other sites
shoz wouldnt it work if the user set a session with the old entries then use a simple update page

Share this post


Link to post
Share on other sites
[quote author=redarrow link=topic=102905.msg409187#msg409187 date=1154639197]
shoz wouldnt it work if the user set a session with the old entries then use a simple update page
[/quote]
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.

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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.