Search in sources :

Example 1 with Trade2

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

the class OptimisticTest method testBasicDateTimeStrategy.

/**
 * Test of conflicting transactions, using "date-time" strategy.
 */
public void testBasicDateTimeStrategy() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_DATASTORE_TIME_STORES_MILLISECS)) {
        LOG.warn("Database doesnt support storing of millisecs in DATETIME columns so ignoring the test");
        return;
    }
    addClassesToSchema(new Class[] { Trade2.class });
    PersistenceManager pm1 = pmf.getPersistenceManager();
    Transaction tx1 = pm1.currentTransaction();
    PersistenceManager pm2 = pmf.getPersistenceManager();
    Transaction tx2 = pm2.currentTransaction();
    try {
        tx1.begin();
        Trade2 t1 = new Trade2("Mr X", 100.0, new Date());
        pm1.makePersistent(t1);
        tx1.commit();
        Object id = pm1.getObjectId(t1);
        // check conflict
        pm1.setIgnoreCache(true);
        tx1.setOptimistic(true);
        tx1.begin();
        t1 = (Trade2) pm1.getObjectById(id, true);
        t1.setPerson("Mr Y");
        pm2.setIgnoreCache(true);
        tx2.setOptimistic(true);
        tx2.begin();
        t1 = (Trade2) pm2.getObjectById(id, true);
        t1.setPerson("Mr Z");
        // commit tx1
        tx1.commit();
        boolean success = false;
        try {
            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(Trade2.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Trade2(org.jpox.samples.versioned.Trade2) 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 Trade2 (org.jpox.samples.versioned.Trade2)1