Search in sources :

Example 1 with Trade1Holder

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);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Trade1(org.jpox.samples.versioned.Trade1) Trade1Holder(org.jpox.samples.versioned.Trade1Holder) Date(java.util.Date)

Example 2 with Trade1Holder

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);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Trade1(org.jpox.samples.versioned.Trade1) Trade1Holder(org.jpox.samples.versioned.Trade1Holder) Date(java.util.Date) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException)

Aggregations

Date (java.util.Date)2 PersistenceManager (javax.jdo.PersistenceManager)2 Transaction (javax.jdo.Transaction)2 Trade1 (org.jpox.samples.versioned.Trade1)2 Trade1Holder (org.jpox.samples.versioned.Trade1Holder)2 JDOOptimisticVerificationException (javax.jdo.JDOOptimisticVerificationException)1