I'm trying to implement lock tables in database with c++ and mysql. i'm using tpc-a benchmarks where i simulate a mini bank database and i run a bunch of threads on it and each thread do 10 transactions concurrently and goal is to maintain consistency after all transaction finished their work. here is a piece of code that is a part of for loop that each thread run 10 times :

g_lockTableAccount->lockRecord(hashInt(randomAccountNumSource, g_numAccount), randomAccountNumSource, GetCurrentThread());
    //get first account balance and branch.

        std::string query = "SELECT account_balance,branch_ID from `account` WHERE account_ID = " + std::to_string(randomAccountNumSource) + ";";
        executeQuery(conn, query);
        MYSQL_RES * res = mysql_use_result(conn);
        MYSQL_ROW row = mysql_fetch_row(res);

        sourceAccountBalance = toInt(row[0]);
        sourceAccountBranch = toInt(row[1]);

    sourceAccountBalance -= amount;
        std::string query = "UPDATE `Account` SET account_balance = '" +
            std::to_string(sourceAccountBalance) + "' where account_ID = " + std::to_string(randomAccountNumSource) + ";";
        executeQuery(conn, query);

    g_lockTableAccount->unlockRecord(hashInt(randomAccountNumSource, g_numAccount), randomAccountNumSource);

i lock and unlock other records with the same pattern. but i feel there is something wrong with it because i think the pattern of running a sql command and committing it is not correct, but the problem is when i start transaction at the beginning of for loop and commit at the end this pattern ensue deadlocks because at any isolation level the database won't release the locks it acquired. how can i do this correctly and without getting deadlocks? is there anyway at all? how different isolation levels affect this problem?

1 Answer 1


In just MySQL, one would do this:

SELECT @bal := balance FROM acct WHERE id=123  FOR UPDATE;
SET @bal = @bal - @amount;
UPDATE acct SET balance = @bal WHERE id = 123;

The FOR UPDATE declares the intention to change the row(s) found. This keeps other threads away -- not via deadlock, but via a delay.

I don't know what is added by lockRecord(). Maybe it is a replacement for FOR UPDATE. If so, it is over-kill since it grabs the row for longer than the MySQL code does.

I don't think transaction_isolation_level matter, but I am not sure.

A deadlock can occur when you grab multiple rows in a transaction while another transaction is grabbing the same rows in a different order. In some situations, the "gap" next to a row counts, and can participate in a deadlock.

  • by usinglockrecord() i want to simulate FOR UPDATE that you've mentioned and after i use FOR UPDATE i think database can manage concurrent transaction on its own without any consistency loss, am i right? May 1, 2019 at 10:30
  • @PeymanTahghighi - Yes, that's the point.
    – Rick James
    May 1, 2019 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.