For example, I have a “Connection1” wants to increase the “currentcount” counter, and then select the value. “Connection2” wants to do the same, and is running at the exact same time. These are the queries that are running (but not as a transaction).
Query 1: UPDATE table SET currentcount=currentcount+1;
Query 2: SELECT currentcount FROM table;
If Connection1 and Connection2 run Query1 at the same time, and then Query2 at the exact same time, then the result of query2 will be the same for both connections (i.e. a race condition).
I have heard that transactions may work in this situation.
How would I solve this problem in mysql?
You need to:
Be aware that this can significantly affect concurrent performance as by necessity you have to prevent the second connection from doing anything at the same time as the first connection (and vice versa). However this should only become an issue if you were performing this operation thousands of times per second.
Also, see my answer to a similar question where there’s a stored procedure that can be trivially modified to make your increment / select cycle a single round trip to the server.