I will document that:
To avoid deadlocks, ensure that all transactions lock the tables in
the same order (for example in alphabetical order), and avoid
upgrading read locks to write locks. Both can be achieved using
explicitly locking tables using SELECT ... FOR UPDATE.
See also http://en.wikipedia.org/wiki/Deadlock#Prevention
Regards,
Thomas
> So am I to understand that H2 provides no way to request a read
> (shared) lock in an explicit order? I have to get a write (exclusive)
> lock every time I want to do a join?
Do you mean you want to lock multiple tables in a specific order using
one SQL statement? No, currently this is not supported. You need to
use multiple SQL statements. In theory you could combine them into one
statement as follows, but this is a hack, and I'm not sure if it will
be supported in future versions:
Statement stat = ...
stat.execute("select * from a for update; select * from b for update");
Regards,
Thomas
> Well, actually what I want is the ability to lock multiple tables in a
> specific order using a single SQL command, but with a shared lock, not
> an exclusive lock.
I don't know how other databases do that. For H2, you could use:
Statement stat = ...
stat.execute("select * from a where 1=0; select * from b where 1=0");
Regards,
Thomas
I will add a feature request for LOCK TABLES. If you need it, please
submit a patch.
Regards,
Thomas