Search in sources :

Example 1 with Trade4Sub

use of org.jpox.samples.versioned.Trade4Sub in project tests by datanucleus.

the class OptimisticTest method testBasicVersionNumberStrategyVersionFieldForSubclass.

/**
 * Test of conflicting transactions, using "version-number" strategy where the class has a version field.
 */
public void testBasicVersionNumberStrategyVersionFieldForSubclass() {
    PersistenceManager pm1 = pmf.getPersistenceManager();
    Transaction tx1 = pm1.currentTransaction();
    PersistenceManager pm2 = pmf.getPersistenceManager();
    Transaction tx2 = pm2.currentTransaction();
    try {
        tx1.begin();
        Trade4Sub tradeA = new Trade4Sub("Mr S", 200.0, new Date());
        tradeA.setSubName("SubName-1");
        pm1.makePersistent(tradeA);
        pm1.flush();
        assertEquals("Trade4Sub just persisted using makePersistent has incorrect version!", 1, tradeA.getVersion());
        tx1.commit();
        pmf.getDataStoreCache().evictAll();
        Object id = pm1.getObjectId(tradeA);
        // retrieve the object in PM1/txn1
        pm1.setIgnoreCache(true);
        tx1.setOptimistic(true);
        tx1.begin();
        Trade4Sub tradeB = (Trade4Sub) pm1.getObjectById(id, true);
        // retrieve the object in PM2/txn2 (without the change from txn1 presumably)
        pm2.setIgnoreCache(true);
        tx2.setOptimistic(true);
        tx2.begin();
        Trade4Sub tradeC = (Trade4Sub) pm2.getObjectById(id, true);
        // update the object in PM1/txn1
        tradeB.setSubName("SubName-2");
        // commit txn1 with the change
        tx1.commit();
        assertEquals("Version of Trade4Sub is incorrect", 2l, JDOHelper.getVersion(tradeB));
        // Update in txn2
        tradeC.setSubName("SubName-3");
        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) {
        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(Trade4Sub.class);
    }
}
Also used : Trade4Sub(org.jpox.samples.versioned.Trade4Sub) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) Date(java.util.Date) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException)

Aggregations

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