Search in sources :

Example 1 with Trade4

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

Example 2 with Trade4

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

the class OptimisticTest method testGetObjectByIdWithBasicVersionNumberStrategyVersionField.

/**
 * Test of conflicting transactions, using "version-number" strategy where the class has a version field.
 */
public void testGetObjectByIdWithBasicVersionNumberStrategyVersionField() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Trade4 tradeA = new Trade4("Mr X", 100.0, new Date());
        pm.makePersistent(tradeA);
        tx.commit();
        assertEquals("Object just persisted using makePersistent has incorrect version!", 1, tradeA.getVersion());
        Object id = pm.getObjectId(tradeA);
        pmf.getDataStoreCache().evictAll();
        // retrieve the object using getObjectById and check that the version is set
        pm.setIgnoreCache(true);
        tx.setOptimistic(true);
        tx.begin();
        Trade4 tradeB = (Trade4) pm.getObjectById(id);
        assertNotNull(JDOHelper.getVersion(tradeB));
        assertEquals(new Long(1), JDOHelper.getVersion(tradeB));
        tx.commit();
    } catch (Exception ex) {
        LOG.error("Exception in test", ex);
        fail("Exception thrown during test of conflictTransactions: " + ex.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Clean out our data
        clean(Trade4.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Trade4(org.jpox.samples.versioned.Trade4) Date(java.util.Date) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException)

Aggregations

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