My Problem is, that I want to insert a new row with a calculated value dependent on a select, executed earlier in the transaction. But avoid, that parallel transactions insert the same calculated value.
This problem should be avoided through transaction isolation level SERIALIZABLE. But it doesn't.
I run through the following steps:
- TRANSACTION A: BEGIN;
- TRANSACTION B: BEGIN;
- TRANSACTION A: select max(x) from tableName;
- TRANSACTION B: select max(x) from tableName;
- TRANSACTION B: insert into tableName (x, PK_COLUMN) values (max + 1, primaryKeyA);
- TRANSACTION A: insert into tableName (x, PK_COLUMN) values (max + 1, primaryKeyB);
- TRANSACTION A: COMMIT;
- TRANSACTION B: COMMIT;
After this, in MySQL, I have two new rows with the same x value, but I want, that one of both transactions abort.
On PostgreSQL I can do a LOCK TABLE tableName IN SHARE MODE
and this prevents TRANSACTION A to execute step 6. On MySQL I tried the LOCKING techniques described under http://dev.mysql.com/doc/refman/5.1/de/lock-tables.html without success.
Any suggestions? Thanks.