Hi there, I'm using Hibernate 2.1.2 with MySQL 4.0.14.
I have a failry standard class and do something like this to load an
instance:
Code:
Query query = session.getNamedQuery(queryName);
for (int i=0; i < names.length; i++)
query.setParameter(names[i], values[i]);
query.setMaxResults(1);
List list = query.list();
Object o = null;
if (!list.isEmpty())
o = list.get(0);
if (o != null && !LockMode.NONE.equals(state.getLockMode()))
session.lock(o, state.getLockMode());
return o;
I've just noticed the query.uniqueInstance() method, so I'll probably
switch that list logic there, but that's not really relevant at the moment.
Anyway, as you can tell, I'm trying to (pessimistically) lock the object
returned from the query. Obviously there is a race condition here,
where you can have two (or more) threads each doing the read,
AND THEN the obtaining the lock - but it's too late, one thread already
has bad data.
Is there any way I can set the LockMode (specifically UPGRADE) when
doing a query? Or do I have to re-organize all the code to do
optimistic locking with a pessimistic fallback (or something)?
Thanks
Hunter