Reputation: 17314
I'm rewriting this question because it got no responses.
I'm trying to figure out the correct way to work with db transactions. Everything I see about how to do transactions is very basic, along the lines of:
I get that, but it's error (deadlock) handling I don't get. I've heard of two options:
To me, telling the user to try again because of a technical issue like this seems bad - do real applications do this regularly? Is that the "oops, something went wrong" one-off errors I sometimes see? This is for a website, so users shouldn't even be aware of the database.
So I have a few questions:
Upvotes: 3
Views: 352
Reputation: 11478
There's a couple things listed here that you didn't mention:
http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html
Upvotes: 0
Reputation: 30496
The solution I use is having a retry loop, let's say 3 (I don't think I get more than 1 retry in real life of most apps). In this loop all the request of the transaction must be done, i.e. write request but as well read requests.
One very important point is to do theread requests inside the transaction as this set the locks and it is the only way to have the data at th ereal isolation level.
Then all these requests are in a begin/commit block, with a try/catch. In the catch section I run the rollback and I rethrow the exception.
By re-throwing the exception you can catch it in higher level, which is the level where you can decide to re-run (the 3 times loop) or send it to the user.
Depending on the way your application is coded there are several solutions to handle this in a nice way:
Upvotes: 1
Reputation: 60498
A deadlock during a transaction would usually indicate that you were trying to update something that someone else also updated at the same time as you. Exactly how you handle that would be specific to the operation that the user was performing.
In some cases it wouldn't be a problem and you would retry the operation, but I would think that in most cases you would generate an error to the user since you have know way of knowing which update was the "correct" one.
Upvotes: 0