Ok. Since
the discussion last night I have been doing some thinking, reading and testing. I also had a glance in HIA.
It seems there are two different roots for the problems I am seeing.
The first root is that, for historical reasons, we do not use optimistic locking with versioning or timestamps. When the project started, I was the only one with experience of OO and SQL. It has been a long, and sometimes adventurous, road to move the development from function orientation and "raw" JDBC to OO and proprietary ORM. Adding versioning to the older ("raw") parts of the system would be very cumbersome.
The other root of the problem is the way MySQL implements the repeatable read isolation level. We have to avoid what Hibernate in Action calls the "second lost updates problem" (and as I said, without versioning). HIA claims that repeatable read is a sufficient isolation level for avoiding this problem. Today I found that for PostgreSQL and Mimer SQL indeed it is - PostgreSQL for example gives "ERROR: could not serialize access due to concurrent update" (since PostgreSQL uses serializable for the repeatable read setting).
But for MySQL (InnoDB) it is not. To avoid the "second lost updates problem" (without versioning) you have to use explicit pessimistic locking or serializable isolation level (which causes implicit pessimistic locking).
So, as I see it, there are three theoretically possible solutions. Lower the global isolation level to repeatable read and...
1. add versioning/timestamps
2. add explicit pessimistic locking where needed
3. increase isolation level to serializable (and thus adding implicit pessimistic locking) where needed
Number 1 is probably out of the question, since we would have a lot of redesigning to do. (The app need redesign anyway, but it will have to wait for the next generation).
Number 2 is quite feasible. Since I have marked all updating methods (i.e. methods that need more isolation) with annotations (used for AOP) and all JDBC calls go through a wrapper, I could add "FOR UPDATE" in this wrapper. BUT this adds problems should we start using Hibernate. Then we would either have to start adding locking explicitly on every lookup (which the project manager wants to avoid), or find a way to intercept Hibernate lookups and add locking (which should be possible with AOP, but it seems Hibernate Interceptor is not of much help).
Number 3 would probably be the easiest to implement, but I'm not sure it would actually work (since there
might be database access before the isolation level change in some instances).
Any thoughts here will be appreciated.
Essentially this seems to be more of a MySQL issue than a Hibernate issue, and I would like to know how other users of MySQL have resolved this. But when Googling for "second lost updates problem" all I get is the HIA index at Manning.
So my bottom line question is: are there any other names for the "second lost updates problem", that I can use for searching more info?