use of org.jpox.samples.versioned.Trade1 in project tests by datanucleus.
the class OptimisticTest method testOptimisticJoinTableRelation.
/**
* Tests the persistence of data using a 1-N join table relation with both classes using optimistic txns.
*/
public void testOptimisticJoinTableRelation() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.setOptimistic(true);
try {
tx.begin();
Trade1Holder block = new Trade1Holder("Bearings");
pm.makePersistent(block);
Trade1 t1 = new Trade1("Mr X", 100.0, new Date());
block.addTrade(t1);
Trade1 t2 = new Trade1("Mr Y", 500.0, new Date());
block.addTrade(t2);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
// Clean out our data
clean(Trade1Holder.class);
clean(Trade1.class);
}
}
use of org.jpox.samples.versioned.Trade1 in project tests by datanucleus.
the class OptimisticTest method testSCOContainerClear.
/**
* Test of conflicting transactions with use of SCO collection clear() method.
* Could also use remove() method on collection for same result.
* Tests that SCO methods are caught by the optimistic process.
*/
public void testSCOContainerClear() {
PersistenceManager pm1 = pmf.getPersistenceManager();
Transaction tx1 = pm1.currentTransaction();
pm1.setIgnoreCache(true);
tx1.setOptimistic(true);
PersistenceManager pm2 = pmf.getPersistenceManager();
Transaction tx2 = pm2.currentTransaction();
pm2.setIgnoreCache(true);
tx2.setOptimistic(true);
try {
Object id = null;
// Persist the object
tx1.begin();
Trade1Holder block = new Trade1Holder("First block");
Trade1 t1 = new Trade1("Tony Blair", 200.00, new Date());
block.addTrade(t1);
pm1.makePersistent(block);
tx1.commit();
id = pm1.getObjectId(block);
// TXN1 : retrieve the object and update a field
tx1.begin();
Trade1Holder block1 = (Trade1Holder) pm1.getObjectById(id);
block1.setName("First block modified");
// TXN2 : retrieve the object
tx2.begin();
Trade1Holder block2 = (Trade1Holder) pm2.getObjectById(id);
// TXN1 : commit the change
tx1.commit();
// TXN2 : clear the collection of trades
// This should NOT throw exceptions (particularly not OptimisticVerificationExceptions)
block2.clearTrades();
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception thrown during test of SCO collection clear when should have left it until commit: " + ex.getMessage());
} finally {
if (tx1.isActive()) {
tx1.rollback();
}
pm1.close();
if (tx2.isActive()) {
tx2.rollback();
}
pm2.close();
// Clean out our data
clean(Trade1Holder.class);
clean(Trade1.class);
}
}
use of org.jpox.samples.versioned.Trade1 in project tests by datanucleus.
the class OptimisticTest method testConflictTransactionsVersionNumberFromQuery.
/**
* Test of conflicting transactions, using "version-number" strategy, when the
* object is retrieved via a query.
*/
public void testConflictTransactionsVersionNumberFromQuery() {
PersistenceManager pm1 = pmf.getPersistenceManager();
Transaction tx1 = pm1.currentTransaction();
PersistenceManager pm2 = pmf.getPersistenceManager();
Transaction tx2 = pm2.currentTransaction();
try {
// Persist the object
tx1.begin();
Trade1 t1 = new Trade1("Mr X", 100.0, new Date());
pm1.makePersistent(t1);
tx1.commit();
// retrieve the object
pm1.setIgnoreCache(true);
tx1.setOptimistic(true);
tx1.begin();
Query q1 = pm1.newQuery(Trade1.class, "person == \"Mr X\"");
List results = (List) q1.execute();
t1 = (Trade1) results.get(0);
assertNotNull("Optimistic version is null", JDOHelper.getVersion(t1));
t1.setPerson("Mr Y");
pm2.setIgnoreCache(true);
tx2.setOptimistic(true);
tx2.begin();
Query q2 = pm2.newQuery(Trade1.class, "person == \"Mr X\"");
results = (List) q2.execute();
t1 = (Trade1) results.get(0);
assertNotNull("Optimistic version is null", JDOHelper.getVersion(t1));
t1.setPerson("Mr Z");
// commit tx1
tx1.commit();
boolean success = false;
try {
tx2.commit();
} catch (JDOOptimisticVerificationException ove) {
success = true;
}
assertTrue("JDOOptimisticVerificationException expected", success);
assertFalse("transaction should be rolledback", tx2.isActive());
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception thrown during test of conflictTransactions via query: " + ex.getMessage());
} finally {
if (tx1.isActive()) {
tx1.rollback();
}
pm1.close();
if (tx2.isActive()) {
tx2.rollback();
}
pm2.close();
// Clean out our data
clean(Trade1.class);
}
}
use of org.jpox.samples.versioned.Trade1 in project tests by datanucleus.
the class OptimisticTest method testMultipleUpdates.
/**
* Test two succeeding updates in the same transaction, with the
* first updating the version in the datastore before the second is made and committed
*/
public void testMultipleUpdates() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Trade1 t1 = new Trade1("Mr Smith", 1400.0, new Date());
pm.makePersistent(t1);
tx.commit();
Object id = pm.getObjectId(t1);
// check conflict
tx.setOptimistic(true);
tx.begin();
t1 = (Trade1) pm.getObjectById(id, true);
t1.setPerson("Mr Jones");
pm.flush();
t1.setPerson("Mr Green");
pm.flush();
t1.setPerson("Mr Arbuthnot");
tx.commit();
// not seeing a JDOOptimisticVerificationException is the success here
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception thrown during test of conflictTransactions: " + ex.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
// Clean out our data
clean(Trade1.class);
}
}
use of org.jpox.samples.versioned.Trade1 in project tests by datanucleus.
the class OptimisticTest method testDeleteAfterUpdate.
/**
* Test delete after update
*/
public void testDeleteAfterUpdate() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.setOptimistic(true);
try {
// Insert
tx.begin();
Trade1 t1 = new Trade1("Mr X", 100.0, new Date());
pm.makePersistent(t1);
tx.commit();
Object id = pm.getObjectId(t1);
tx.begin();
t1 = (Trade1) pm.getObjectById(id);
assertEquals(new Long(1), JDOHelper.getVersion(t1));
tx.commit();
// Delete - what the test is for
tx.begin();
t1 = (Trade1) pm.getObjectById(id, true);
t1.setPerson("Mr Y");
pm.flush();
pm.deletePersistent(t1);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
// Clean out our data
clean(Trade1.class);
}
}
Aggregations