Your solution is not concurrency-safe. The increment of a value in a database should be performed as an atomic operation.
Suppose the value to be incremented is X. By reading, incrementing and writing as separate operations, the following is very possible:[ol][li]One user reads the value, and gets X.[/li][li]A second user reads the value and gets X.[/li][li]The first user increments the value to X + 1 then writes the new value to the database.[/li][li]The second user then increments the value to X + 1 and does the name.[/li][/ol]
Now the value in the database is X + 1, when it should be X + 2.
Most SQL-based database servers will allow an increment to be performed as such:
UPDATE tablename SET columnname = columnname + 1 WHERE anothercolumn = somecondition. Want the best answers? Ask the best questions:
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.