p5oid = pm1.getObjectId(p5);
tx1.commit();
// update/delete the instances in tx1
tx1.begin();
VersionedPCPoint p1tx1 = (VersionedPCPoint)pm1.getObjectById(p1oid, true);
VersionedPCPoint p2tx1 = (VersionedPCPoint)pm1.getObjectById(p2oid, true);
VersionedPCPoint p3tx1 = (VersionedPCPoint)pm1.getObjectById(p3oid, true);
VersionedPCPoint p4tx1 = (VersionedPCPoint)pm1.getObjectById(p4oid, true);
p1tx1.setX(101);
p2tx1.setX(201);
pm1.deletePersistent(p3tx1);
pm1.deletePersistent(p4tx1);
// update/delete the instances in tx2
tx2.begin();
VersionedPCPoint p1tx2 = (VersionedPCPoint)pm2.getObjectById(p1oid, true);
VersionedPCPoint p2tx2 = (VersionedPCPoint)pm2.getObjectById(p2oid, true);
VersionedPCPoint p3tx2 = (VersionedPCPoint)pm2.getObjectById(p3oid, true);
VersionedPCPoint p4tx2 = (VersionedPCPoint)pm2.getObjectById(p4oid, true);
VersionedPCPoint p5tx2 = (VersionedPCPoint)pm2.getObjectById(p5oid, true);
p1tx2.setX(102);
pm2.deletePersistent(p2tx2);
p3tx2.setX(202);
pm2.deletePersistent(p4tx2);
p5tx2.setX(502); // this change must not be committed
Set expectedFailedObjects = new HashSet();
expectedFailedObjects.add(p1tx2);
expectedFailedObjects.add(p2tx2);
expectedFailedObjects.add(p3tx2);
expectedFailedObjects.add(p4tx2);
// commit tx1 (should succeed)
tx1.commit();
tx1 = null;
// commit tx2 (should fail)
try {
tx2.commit();
fail(ASSERTION_FAILED, "concurrent commit not detected");
}
catch (JDOOptimisticVerificationException ex) {
// verify the correct information in the exception
Throwable[] ts = ex.getNestedExceptions();
int length = ts==null ? 0 : ts.length;
int expectedFailures = expectedFailedObjects.size();
if (length != expectedFailures) {
fail(ASSERTION_FAILED,
"Nested exceptions[] wrong size: expected " +
expectedFailures + ", got " + length);
}
for (int i = 0; i < length; ++i) {
Throwable t = ts[i];
if (t instanceof JDOOptimisticVerificationException) {
if (debug)
logger.debug("Expected exception caught " + t.toString());
JDOException jex = (JDOException)t;
Object failed = jex.getFailedObject();
if (failed == null) {
fail(ASSERTION_FAILED,
"Found unexpected null in failed object");
}
else {
if (expectedFailedObjects.remove(failed)) {
if (debug)
logger.debug("Found expected failed instance, oid: " +
JDOHelper.getObjectId(failed));
}
else {
fail(ASSERTION_FAILED,
"Unexpected failed instance: " + failed.toString());
}
}
}
else {
fail(ASSERTION_FAILED,
"Unexpected nested exception: " + t.toString());
}
}
}
tx2 = null;
tx3.begin();
VersionedPCPoint p1tx3 = (VersionedPCPoint)pm3.getObjectById(p1oid, true);
VersionedPCPoint p2tx3 = (VersionedPCPoint)pm3.getObjectById(p2oid, true);
VersionedPCPoint p5tx3 = (VersionedPCPoint)pm3.getObjectById(p5oid, true);
verify(p1tx3, 101);
verify(p2tx3, 201);
verify(p5tx3, 5);
tx3.commit();
tx3 = null;