2

I'm currently trying to improve the testing behaviour of our application.

The tests are currently started within nested transactions, where the test suite start the actual transaction and the test cases each create nested savepoints.

When a test case is finished the savepoint is simply released and the original data is restored. When the suite is finished the transaction is rolled back.

(Using truncation on the tables is not an option for us, since the seed data is just to big and it prevents central and concurrent tests)

This works fine.

When the transaction run concurrently, the transactions tend to lock each other out of tables. This happens even more so on tables with an unique key (every test fails for one of the two transactions).

I tried to do this with the transaction states "READ COMMITTED", "READ UNCOMMITED" and the default "REPEATABLE READ".

READ UNCOMMITTED resulted in duplicate key issues. READ COMMITTED has lock / deadlock issues REPEATABLE READ has lock / deadlock issues (same as above)

Additionally i tried to apply the following options: innodb_autoinc_lock_mode=2

> -- mysql command line
> SET SESSION binlog_format=ROW
> SET SESSION innodb_table_locks=OFF

The last parameter solved some locking issues. The "lock wait timeout" and deadlock issues remain.

Is there a solution the make seperate transactions truly independent of each other?

update: further testing showed: The deadlocks currently only happen on tables with unique keys. I think session 1 already used those keys, so i might be a victim of row locking.

mysql server version: 5.5.15

Edit: adding workflow example, so the problem is clearer stated, (might contain typos and minor syntax errors. those are not the point). different connections differently intended

> create table foo (
>   `id` int(11) auto increment,
>   `name` varchar(255),
>   PRIMARY KEY id,
>   UNIQUE KEY `name`(`name`)
> ) ENGINE=InnoDb;
> insert into foo(name) values ('bar');

> begin;                                       -- connection 1, transaction 1
>   begin;                                     -- connection 2, transaction 1
> savepoint t_1;                               -- connection 1, savepoint within transaction 1
>   savepoint t_1;                             -- connection 2, savepoint within transaction 1
> select count(*) from foo;                    -- connection 1, number of entries in foo, should be 1
> insert into foo('name') values ('meh');      -- connection 1, insert into foo the unique fixed value 'meh', should work
>   select count(*) from foo;                  -- connection 2, number of entries in foo, should be 1
>   insert into foo('name') values ('meh');    -- connection 2, insert into foo the unique fixed value 'meh', should work
> select count(*) from foo;                    -- connection 1, number of entries in foo, should be 2
>   select count(*) from foo;                  -- connection 2, number of entries in foo, should be 2
> rollback to savepoint t_1;                       -- connection 1, discard everything up until savepoint t_1
> rollback;                                    -- connection 1, rollback
>   rollback to savepoint t_1;                     -- connection 2, discard everything up until savepoint t_1
>   rollback;                                  -- connection 2, rollback

In this example all the should be are to be considered tests for the transaction isolation i want.

1 Answer 1

0

It sounds like you're trying to run the tests at the same time over the top of each other, which is why there are concurrent queries going on. If that's the case, then I think you're 100% correct in that you're simply experiencing locks.

If there is an open transaction (i.e. a long running test), with the intended purpose of being rolled back when the test ends, you'll have a lot of trouble making any other writes and potentially some reads on those same rows/tables as part of other tests. Your best bet is to probably just run everything sequentially.

The issues with duplicate keys that you're experiencing is consistent with what I would expect when using READ UNCOMMITTED.

4
  • yes, you understood me correctly, that's what i am trying to do. But running in parallel is what i am trying to achieve. in a nutshell: i want for two open transactions the possibility to insert into the same table (that has a unique key) without running into locks or duplicate key issues.
    – scones
    Jan 27, 2013 at 1:19
  • 1
    That should not be an issue, is the unique key an auto inc? If not, you simply need to make sure you generate a unique value prior to doing the insert. Using some kind of GUID should solve the problem, or just use the built in auto inc feature. Sorry for the slow reply. Feb 2, 2013 at 19:21
  • For more info on GUIDs check this out: guidgenerator.com/online-guid-generator.aspx I would really just use the auto inc feature however. Feb 2, 2013 at 19:24
  • sadly that's the fallback plan i had devised, when the problem was not solvable by mysql-means. this will require rewriting the test framework method of generating serialized numbers. This is as much fun as finding a good random seed.
    – scones
    Feb 2, 2013 at 19:41

Your Answer

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

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