use of org.jpox.samples.versioned.Trade1Holder 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.Trade1Holder 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);
}
}
Aggregations