Search in sources :

Example 1 with Trade6Holder

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

the class OptimisticTest method testOptimisticWithIdentityFieldObjects.

/**
 * Test of use of autoincrement ids with optimistic transactions.
 * Autoincrement ids are generated in the datastore and so will call flush() from the outset.
 */
public void testOptimisticWithIdentityFieldObjects() throws Exception {
    if (!storeMgr.supportsValueGenerationStrategy("identity")) {
        // Lets just say it passed
        return;
    }
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setOptimistic(true);
        try {
            tx.begin();
            Trade6Holder t6a = new Trade6Holder("NASDAQ 1");
            Trade6Holder t6b = new Trade6Holder("FTSE 2");
            Trade6Holder t6c = new Trade6Holder("CAC 5");
            pm.makePersistent(t6a);
            pm.makePersistent(t6b);
            pm.makePersistent(t6c);
            HashSet c = new HashSet();
            c.add(new Trade6("Donald Duck", 123.45, new Date()));
            c.add(new Trade6("Mickey Mouse", 234.5, new Date()));
            t6a.setTrades(c);
            c = new HashSet();
            c.add(new Trade6("Yogi Bear", 2300.0, new Date()));
            c.add(new Trade6("Minnie Mouse", 1.0, new Date()));
            t6b.setTrades(c);
            c = new HashSet();
            c.add(new Trade6("Barney Rubble", 1245.0, new Date()));
            c.add(new Trade6("Fred Flintstone", 2.0, new Date()));
            t6c.setTrades(c);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error("Exception thrown during test of autoincrement with flushed objects");
            LOG.error(e);
            fail("Exception thrown during test of autoincrement with flushed objects : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent ex = pm.getExtent(Trade6.class);
            Iterator iter = ex.iterator();
            while (iter.hasNext()) {
                Trade6 tr6 = (Trade6) iter.next();
                if (tr6.getHolder() != null) {
                    Trade6Holder holder = tr6.getHolder();
                    tr6.setHolder(null);
                    holder.setTrades(null);
                }
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        clean(Trade6Holder.class);
        clean(Trade6.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) Trade6(org.jpox.samples.versioned.Trade6) Trade6Holder(org.jpox.samples.versioned.Trade6Holder) Date(java.util.Date) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) HashSet(java.util.HashSet)

Aggregations

Date (java.util.Date)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Extent (javax.jdo.Extent)1 JDOOptimisticVerificationException (javax.jdo.JDOOptimisticVerificationException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 Trade6 (org.jpox.samples.versioned.Trade6)1 Trade6Holder (org.jpox.samples.versioned.Trade6Holder)1