Hi there,
Does anybody know what the correct way would be in order to do the following. I'm using MYSQL since the last 4 months, and MYSQL does have a different way of processing locks than other engines I have used before.
Basically I need to read a record/s out of a table, and after getting the detail of these records, update them to a state to not allow re-use.
Now, my application is a multi-threaded environment, so concurrency is an issue. First I thought of handling mutex's in my application, but since MYSQL supports record locking, I thought I'd give it a bash.
I currently do the following on each different Thread connection:
1 - set autocommit=0;
2 - set session transaction isolation level READ COMMITTED;
3 - START TRANSACTION;
4 - SELECT * FROM Item WHERE Status = 0 AND BuyProduct = 7 LIMIT 5 FOR UPDATE;
5 - [Loop through above recordset and update] UPDATE Item SET Status = 1 WHERE id = ?
6 - COMMIT
Now imagine 1000 threads 'attacking' this table at the same time....
I get a lot of MySQLTransactionRollbackExceptions because numerous threads are attempting to update the same records, due from the select clause. Even though I specify FOR UPDATE?
Does anybody have some advice?
W
Does anybody know what the correct way would be in order to do the following. I'm using MYSQL since the last 4 months, and MYSQL does have a different way of processing locks than other engines I have used before.
Basically I need to read a record/s out of a table, and after getting the detail of these records, update them to a state to not allow re-use.
Now, my application is a multi-threaded environment, so concurrency is an issue. First I thought of handling mutex's in my application, but since MYSQL supports record locking, I thought I'd give it a bash.
I currently do the following on each different Thread connection:
1 - set autocommit=0;
2 - set session transaction isolation level READ COMMITTED;
3 - START TRANSACTION;
4 - SELECT * FROM Item WHERE Status = 0 AND BuyProduct = 7 LIMIT 5 FOR UPDATE;
5 - [Loop through above recordset and update] UPDATE Item SET Status = 1 WHERE id = ?
6 - COMMIT
Now imagine 1000 threads 'attacking' this table at the same time....
I get a lot of MySQLTransactionRollbackExceptions because numerous threads are attempting to update the same records, due from the select clause. Even though I specify FOR UPDATE?
Does anybody have some advice?
W