0

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:

  1. TRANSACTION A: BEGIN;
  2. TRANSACTION B: BEGIN;
  3. TRANSACTION A: select max(x) from tableName;
  4. TRANSACTION B: select max(x) from tableName;
  5. TRANSACTION B: insert into tableName (x, PK_COLUMN) values (max + 1, primaryKeyA);
  6. TRANSACTION A: insert into tableName (x, PK_COLUMN) values (max + 1, primaryKeyB);
  7. TRANSACTION A: COMMIT;
  8. 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.

4

1 回答 1

0

我发现了问题。MySQL Workbench 不会为每个 sql 窗口派生一个新事务....所以我使用的是唯一一个事务....在使用连接到服务器的两个终端之后。一切都按预期工作。

于 2012-08-17T10:27:53.357 回答