use of javax.jdo.JDOOptimisticVerificationException in project tests by datanucleus.
the class ApplicationIdPersistenceTest method testOptimisticVersionChecks.
/**
* Test optimistic checking of surrogate version.
*/
public void testOptimisticVersionChecks() throws Exception {
try {
Object id = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Organisation o = new Organisation("DataNucleus");
o.setDescription("The company behind this software");
pm.makePersistent(o);
tx.commit();
id = pm.getObjectId(o);
} catch (Exception e) {
LOG.error("Exception during persist", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
PersistenceManager pm1 = pmf.getPersistenceManager();
Transaction tx1 = pm1.currentTransaction();
tx1.begin();
Organisation o1 = (Organisation) pm1.getObjectById(id);
PersistenceManager pm2 = pmf.getPersistenceManager();
Transaction tx2 = pm2.currentTransaction();
tx2.begin();
Organisation o2 = (Organisation) pm2.getObjectById(id);
// Update o1 in tx1 and commit it
try {
o1.setDescription("Global dataservices company");
tx1.commit();
} catch (Exception e) {
LOG.error("Exception during retrieve/update in tx1", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx1.isActive()) {
tx1.rollback();
}
pm1.close();
}
// Update o2 in tx2 and (try to) commit it
try {
o2.setDescription("Global dataservices company number 2");
tx2.commit();
fail("Should have thrown JDOOptimisticVerificationException!");
} catch (Exception e) {
if (e instanceof JDOOptimisticVerificationException) {
// Expected
} else {
LOG.error("Incorrect exception during update in tx2", e);
fail("Incorrect exception thrown when running test " + e.getMessage());
}
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
pm2.close();
}
} finally {
clean(Organisation.class);
}
}
use of javax.jdo.JDOOptimisticVerificationException in project tests by datanucleus.
the class OptimisticTest method testBasicDateTimeStrategy.
/**
* Test of conflicting transactions, using "date-time" strategy.
*/
public void testBasicDateTimeStrategy() {
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_DATASTORE_TIME_STORES_MILLISECS)) {
LOG.warn("Database doesnt support storing of millisecs in DATETIME columns so ignoring the test");
return;
}
addClassesToSchema(new Class[] { Trade2.class });
PersistenceManager pm1 = pmf.getPersistenceManager();
Transaction tx1 = pm1.currentTransaction();
PersistenceManager pm2 = pmf.getPersistenceManager();
Transaction tx2 = pm2.currentTransaction();
try {
tx1.begin();
Trade2 t1 = new Trade2("Mr X", 100.0, new Date());
pm1.makePersistent(t1);
tx1.commit();
Object id = pm1.getObjectId(t1);
// check conflict
pm1.setIgnoreCache(true);
tx1.setOptimistic(true);
tx1.begin();
t1 = (Trade2) pm1.getObjectById(id, true);
t1.setPerson("Mr Y");
pm2.setIgnoreCache(true);
tx2.setOptimistic(true);
tx2.begin();
t1 = (Trade2) pm2.getObjectById(id, true);
t1.setPerson("Mr Z");
// commit tx1
tx1.commit();
boolean success = false;
try {
tx2.commit();
} catch (JDOOptimisticVerificationException ove) {
success = true;
}
assertTrue("JDOOptimisticVerificationException expected", success);
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception thrown during test of conflictTransactions: " + ex.getMessage());
} finally {
if (tx1.isActive()) {
tx1.rollback();
}
pm1.close();
if (tx2.isActive()) {
tx2.rollback();
}
pm2.close();
// Clean out our data
clean(Trade2.class);
}
}
use of javax.jdo.JDOOptimisticVerificationException in project tests by datanucleus.
the class OptimisticTest method testBasicVersionNumberStrategyVersionField.
/**
* Test of conflicting transactions, using "version-number" strategy where the class has a version field.
*/
public void testBasicVersionNumberStrategyVersionField() {
PersistenceManager pm1 = pmf.getPersistenceManager();
Transaction tx1 = pm1.currentTransaction();
PersistenceManager pm2 = pmf.getPersistenceManager();
Transaction tx2 = pm2.currentTransaction();
try {
tx1.begin();
Trade4 tradeA = new Trade4("Mr X", 100.0, new Date());
pm1.makePersistent(tradeA);
pm1.flush();
assertEquals("Object just persisted using makePersistent has incorrect version!", 1, tradeA.getVersion());
tx1.commit();
Object id = pm1.getObjectId(tradeA);
// retrieve the object in PM1/txn1
pm1.setIgnoreCache(true);
tx1.setOptimistic(true);
tx1.begin();
Trade4 tradeB = (Trade4) pm1.getObjectById(id, true);
// retrieve the object in PM2/txn2 (without the change from txn1 presumably)
pm2.setIgnoreCache(true);
tx2.setOptimistic(true);
tx2.begin();
Trade4 tradeC = (Trade4) pm2.getObjectById(id, true);
// update the object in PM1/txn1
tradeB.setPerson("Mr Y");
// commit txn1 with the change
tx1.commit();
// Update in txn2
tradeC.setPerson("Mr Z");
boolean success = false;
try {
// commit txn2 with the change - should throw exception since it has been updated in txn1 first since the read of the object
tx2.commit();
} catch (JDOOptimisticVerificationException ove) {
success = true;
}
assertTrue("JDOOptimisticVerificationException expected", success);
} catch (Exception ex) {
LOG.error("Exception in test", ex);
fail("Exception thrown during test of conflictTransactions: " + ex.getMessage());
} finally {
if (tx1.isActive()) {
tx1.rollback();
}
pm1.close();
if (tx2.isActive()) {
tx2.rollback();
}
pm2.close();
// Clean out our data
clean(Trade4.class);
}
}
use of javax.jdo.JDOOptimisticVerificationException in project tests by datanucleus.
the class OptimisticTest method testBasicNoneStrategy.
/**
* Test of using "none" strategy.
* This should do no actual optimistic checking, although it will update the version column
*/
public void testBasicNoneStrategy() {
PersistenceManager pm1 = pmf.getPersistenceManager();
Transaction tx1 = pm1.currentTransaction();
PersistenceManager pm2 = pmf.getPersistenceManager();
Transaction tx2 = pm2.currentTransaction();
try {
tx1.begin();
Trade3 t1 = new Trade3("Mr X", 100.0, new Date());
pm1.makePersistent(t1);
tx1.commit();
Object id = pm1.getObjectId(t1);
// Check that the version is 1 (JPOX-specific behaviour since JDO2 doesnt define it)
tx1.begin();
t1 = (Trade3) pm1.getObjectById(id);
assertEquals("Version of unversioned object is incorrect after persist", new Long(1), JDOHelper.getVersion(t1));
tx1.commit();
// check conflict between transactions
pm1.setIgnoreCache(true);
tx1.setOptimistic(true);
tx1.begin();
t1 = (Trade3) pm1.getObjectById(id, true);
t1.setPerson("Mr Y");
pm2.setIgnoreCache(true);
tx2.setOptimistic(true);
tx2.begin();
t1 = (Trade3) pm2.getObjectById(id, true);
t1.setPerson("Mr Z");
// commit tx1
tx1.commit();
// Check that the version is 2 (JPOX-specific behaviour since JDO2 doesnt define it).
// THIS IS NOW COMMENTED OUT SINCE BEHAVIOUR REMOVED in 5.1
/*tx1.begin();
t1 = (Trade3)pm1.getObjectById(id);
assertEquals("Version of unversioned object is incorrect after update", new Long(2), JDOHelper.getVersion(t1));
tx1.commit();*/
boolean success = false;
try {
tx2.commit();
} catch (JDOOptimisticVerificationException ove) {
success = true;
}
assertFalse("JDOOptimisticVerificationException not expected", success);
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception thrown during test of conflictTransactions: " + ex.getMessage());
} finally {
if (tx1.isActive()) {
tx1.rollback();
}
pm1.close();
if (tx2.isActive()) {
tx2.rollback();
}
pm2.close();
// Clean out our data
clean(Trade3.class);
}
}
use of javax.jdo.JDOOptimisticVerificationException in project tests by datanucleus.
the class OptimisticTest method testRefreshOfOptimisticObjects.
/**
* Test the use of refresh() on an object that has been updated in another PM.
* It should update the value in the object so it is up to date.
*/
public void testRefreshOfOptimisticObjects() {
try {
Object id1 = null;
// Create some data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Trade1 t1 = new Trade1("Fred Smith", 100.00, new Date());
pm.makePersistent(t1);
tx.commit();
id1 = pm.getObjectId(t1);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
PersistenceManager pm1 = pmf.getPersistenceManager();
Transaction tx1 = pm1.currentTransaction();
PersistenceManager pm2 = pmf.getPersistenceManager();
Transaction tx2 = pm2.currentTransaction();
try {
tx1.setOptimistic(true);
tx2.setOptimistic(true);
tx1.begin();
tx2.begin();
// Retrieve the data (in both PMs)
Trade1 t1pm1 = (Trade1) pm1.getObjectById(id1);
Trade1 t1pm2 = (Trade1) pm2.getObjectById(id1);
// Update it in pm1
t1pm1.setPerson("Fred Smithy");
tx1.commit();
// refresh t1pm2 so it's up to date
pm2.refresh(t1pm2);
assertEquals("Name of person in other pm has just been refreshed and name is incorrect", t1pm2.getPerson(), "Fred Smithy");
tx2.commit();
} catch (JDOOptimisticVerificationException e) {
fail("Exception thrown while refreshing optimistic object : " + e.getMessage());
} finally {
if (tx1.isActive()) {
tx1.rollback();
}
if (tx2.isActive()) {
tx2.rollback();
}
pm1.close();
pm2.close();
}
} finally {
// Clean out our data
clean(Trade1.class);
}
}
Aggregations