I have a test case with two threads executing the following code:
public void run() {
try {
beginTx();
Employee david =
entityManager.getReference(Employee.class, pk);
entityManager.lock(david, LockModeType.WRITE);
entityManager.flush();
Assert.assertTrue(entityManager.contains(david));
Assert.assertEquals(david.getId(), pk);
Assert.assertEquals(david.getFirstName(), "David");
Assert.assertEquals(david.getLastName(), "Blevins");
Assert.assertEquals(1000000.0, david.getSalary());
log("READ");
// wait for other threads to read
try {
startBarrier.await(2, TimeUnit.SECONDS);
} catch (Exception e) {
}
log("WRITE");
david.setSalary(2000000);
} catch (Throwable throwable) {
this.throwable = throwable;
} finally {
try {
commitTx();
} catch (Throwable throwable) {
if (this.throwable == null) this.throwable = throwable;
}
}
}
The first thread successfully reads the row and waits. The second
throws an exception at the entityManager.flush() command after the
write lock. I would assume that when I say "give me a write lock"
OpenJPA would "give me a write lock". Is there anyway to get write
lock in my application for single rows without having to switch
everything to SERIALIZABLE?
-dain