Search in sources :

Example 1 with Trade3

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

the class OptimisticTest method testBasicNoneStrategy.

/**
 * Test of using "none" strategy.
 * This should do no actual optimistic checking, although it will update the version column
 */
public void testBasicNoneStrategy() {
    PersistenceManager pm1 = pmf.getPersistenceManager();
    Transaction tx1 = pm1.currentTransaction();
    PersistenceManager pm2 = pmf.getPersistenceManager();
    Transaction tx2 = pm2.currentTransaction();
    try {
        tx1.begin();
        Trade3 t1 = new Trade3("Mr X", 100.0, new Date());
        pm1.makePersistent(t1);
        tx1.commit();
        Object id = pm1.getObjectId(t1);
        // Check that the version is 1 (JPOX-specific behaviour since JDO2 doesnt define it)
        tx1.begin();
        t1 = (Trade3) pm1.getObjectById(id);
        assertEquals("Version of unversioned object is incorrect after persist", new Long(1), JDOHelper.getVersion(t1));
        tx1.commit();
        // check conflict between transactions
        pm1.setIgnoreCache(true);
        tx1.setOptimistic(true);
        tx1.begin();
        t1 = (Trade3) pm1.getObjectById(id, true);
        t1.setPerson("Mr Y");
        pm2.setIgnoreCache(true);
        tx2.setOptimistic(true);
        tx2.begin();
        t1 = (Trade3) pm2.getObjectById(id, true);
        t1.setPerson("Mr Z");
        // commit tx1
        tx1.commit();
        // Check that the version is 2 (JPOX-specific behaviour since JDO2 doesnt define it).
        // THIS IS NOW COMMENTED OUT SINCE BEHAVIOUR REMOVED in 5.1
        /*tx1.begin();
            t1 = (Trade3)pm1.getObjectById(id);
            assertEquals("Version of unversioned object is incorrect after update", new Long(2), JDOHelper.getVersion(t1));
            tx1.commit();*/
        boolean success = false;
        try {
            tx2.commit();
        } catch (JDOOptimisticVerificationException ove) {
            success = true;
        }
        assertFalse("JDOOptimisticVerificationException not 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(Trade3.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Trade3(org.jpox.samples.versioned.Trade3) 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 Trade3 (org.jpox.samples.versioned.Trade3)1