Hacker News new | past | comments | ask | show | jobs | submit login

It really isn't as easy as it sounds, is it? A better approach might be to UPDATE promo_codes SET used = 1 WHERE ... AND used = 0, and then check that one update occurred. Or if your db supports it, SELECT ... FOR UPDATE OF ... and then update if the code is not yet used. You want the check and the update to occur atomically. And this varies depending on databases, for example in Oracle even if you explicitly begin a transaction, a select does not lock the row (unless you specify FOR UPDATE OF) so if you do the check and the update in separate statements, you still have a race condition).



I actually wasn't personally involved in the solution, but I believe that we went with something involving SELECT FOR UPDATE.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: