}
public void testConcurrentWrites2() {
logger.log(BasicLevel.INFO, "test concurrent writes 2");
// setup
BasicA ba = new BasicA();
ba.writeF1("1");
ba.writeF2(2);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistent(ba);
final Object id = pm.getObjectId(ba);
Assert.assertNotNull("Null object identifier", id);
pm.currentTransaction().commit();
pm.close();
ba = null;
// begin transaction 1
PersistenceManager pm1 = pmf.getPersistenceManager();
logger.log(BasicLevel.INFO, "Thread1: begin");
pm1.currentTransaction().begin();
logger.log(BasicLevel.INFO, "Thread1: getObjectById");
ba = (BasicA) pm1.getObjectById(id, true);
Assert.assertNotNull("Object not found", ba);
// begin transaction 2
Thread t = assertLock(new Runnable () {
public void run () {
PersistenceManager pm2 = pmf.getPersistenceManager();
try {
logger.log(BasicLevel.INFO, "Thread2: begin");
pm2.currentTransaction().begin();
logger.log(BasicLevel.INFO, "Thread2: getObjectById");
BasicA ba = (BasicA) pm2.getObjectById(id, true);
Assert.assertNotNull("Object not found", ba);
logger.log(BasicLevel.INFO, "Thread2: write");
ba.writeF2(ba.readF2() + 1);
logger.log(BasicLevel.INFO, "Thread2: write done");
logger.log(BasicLevel.INFO, "Thread2: commit");
pm2.currentTransaction().commit();
} catch (JDOFatalException e) {
} finally {
pm2.close();
}
}
});
// end transaction 1
logger.log(BasicLevel.INFO, "Thread1: write");
boolean ok = true;
try {
ba.writeF2(ba.readF2() + 1);
ok = true;
logger.log(BasicLevel.INFO, "Thread1: write done");
logger.log(BasicLevel.INFO, "Thread1: commit");
pm1.currentTransaction().commit();
} catch (JDOException e) {
logger.log(BasicLevel.INFO, "Thread1: write aborted"
+ ExceptionHelper.getNested(e).getMessage());
ok = false;
} finally {
pm1.close();
}
assertUnlock(t);
// clean up
pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
ba = (BasicA) pm.getObjectById(id, true);
Assert.assertNotNull("Object not found", ba);
Assert.assertEquals("Bad f1 value", "1", ba.readF1());
Assert.assertEquals("Bad f2 value", ( ok ? 4 : 3), ba.readF2());
pm.deletePersistent(ba);
pm.currentTransaction().commit();
pm.close();
}