Search in sources :

Example 1 with Trade1

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

the class OptimisticTest method testVersionFromLevel2Cache.

/**
 * Test of putting object into L2 cache via a query, and retrieving making sure the version is still set.
 */
public void testVersionFromLevel2Cache() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object id = null;
        try {
            tx.begin();
            Trade1 t1 = new Trade1("Mr X", 100.0, new Date());
            pm.makePersistent(t1);
            pm.flush();
            assertNotNull("Object just persisted using makePersistent doesnt have a version!", JDOHelper.getVersion(t1));
            tx.commit();
            id = pm.getObjectId(t1);
            assertEquals("Version is incorrect", new Long(1), JDOHelper.getVersion(t1));
        } catch (Exception e) {
            LOG.error("Exception in persist", e);
            fail("Exception persisting objects");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        pmf.getDataStoreCache().evictAll();
        pmf.getDataStoreCache().pinAll(true, Trade1.class);
        try {
            // retrieve using query (puts object in L2 cache)
            tx.begin();
            Query q = pm.newQuery(Trade1.class);
            Iterator<Trade1> iter = ((List) q.execute()).iterator();
            while (iter.hasNext()) {
                Trade1 t1 = iter.next();
                assertEquals("Version is incorrect", new Long(1), JDOHelper.getVersion(t1));
            }
            tx.commit();
        } catch (Exception ex) {
            LOG.error("Exception during query", ex);
            fail("Exception thrown during query of object: " + ex.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            // retrieve using id (gets object from L2 cache)
            tx.begin();
            Trade1 t1 = (Trade1) pm.getObjectById(id);
            assertEquals("Version is incorrect", new Long(1), JDOHelper.getVersion(t1));
            tx.commit();
        } catch (Exception ex) {
            LOG.error("Exception during retrieval by id", ex);
            fail("Exception thrown during retrieval by id: " + ex.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Trade1.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Trade1(org.jpox.samples.versioned.Trade1) List(java.util.List) Date(java.util.Date) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException)

Example 2 with Trade1

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

the class OptimisticTest method testCreationUpdateVersionColumns.

/**
 * tests the creation and update of version/timestamp optimistic columns
 * outside an optimistic transaction
 */
public void testCreationUpdateVersionColumns() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // check insert
        tx.begin();
        Trade1 t1 = new Trade1("Mr X", 100.0, new Date());
        pm.makePersistent(t1);
        tx.commit();
        Object id = pm.getObjectId(t1);
        tx.begin();
        t1 = (Trade1) pm.getObjectById(id);
        assertEquals(new Long(1), JDOHelper.getVersion(t1));
        tx.commit();
        // check update
        tx.begin();
        t1 = (Trade1) pm.getObjectById(id, true);
        t1.setPerson("Mr Y");
        tx.commit();
        tx.begin();
        t1 = (Trade1) pm.getObjectById(id);
        assertEquals(new Long(2), JDOHelper.getVersion(t1));
        tx.commit();
        // check update with rollback
        tx.begin();
        t1 = (Trade1) pm.getObjectById(id, true);
        t1.setPerson("Mr Z");
        // this flushes the changes to the database; the column version is incremented
        pm.flush();
        assertEquals(new Long(3), JDOHelper.getVersion(t1));
        // changes are rolled back; the column version keeps the old value
        tx.rollback();
        tx.begin();
        t1 = (Trade1) pm.getObjectById(id);
        assertEquals(new Long(2), JDOHelper.getVersion(t1));
        tx.commit();
        // ---------------------
        // check update + update
        // ---------------------
        tx.begin();
        t1 = (Trade1) pm.getObjectById(id, true);
        t1.setPerson("Mr A");
        tx.commit();
        tx.begin();
        t1 = (Trade1) pm.getObjectById(id);
        assertEquals(new Long(3), JDOHelper.getVersion(t1));
        tx.commit();
        tx.begin();
        t1 = (Trade1) pm.getObjectById(id, true);
        t1.setPerson("Mr B");
        tx.commit();
        tx.begin();
        t1 = (Trade1) pm.getObjectById(id);
        assertEquals(new Long(4), JDOHelper.getVersion(t1));
        tx.commit();
        // ---------------------
        // check update + update with instance in cache
        // ---------------------
        tx.begin();
        t1.setPerson("Mr C");
        tx.commit();
        tx.begin();
        t1 = (Trade1) pm.getObjectById(id);
        assertEquals(new Long(5), JDOHelper.getVersion(t1));
        tx.commit();
        tx.begin();
        t1.setPerson("Mr D");
        tx.commit();
        tx.begin();
        t1 = (Trade1) pm.getObjectById(id);
        assertEquals(new Long(6), JDOHelper.getVersion(t1));
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Clean out our data
        clean(Trade1.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Trade1(org.jpox.samples.versioned.Trade1) Date(java.util.Date)

Example 3 with Trade1

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

the class OptimisticTest method testGetObjectByIdUsingVersionNumberStrategy.

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

Example 4 with Trade1

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

the class OptimisticTest method testRetrieveAfterPersistBeforeFlush.

/**
 * Test whether we can find an object after persist but before flush, using its id.
 * This seems to be totally JPOX-specific and not in the JDO spec.
 * If this behaviour is in the JDO spec mention where this is defined.
 */
public void testRetrieveAfterPersistBeforeFlush() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setOptimistic(true);
        try {
            tx.begin();
            Trade1 t = new Trade1("Mr X", 100.0, new Date());
            pm.makePersistent(t);
            pm.flush();
            Object id = pm.getObjectId(t);
            Object obj = pm.getObjectById(id);
            assertNotNull("getObjectById returned null object even though just persisted", obj);
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("Exception thrown during test of retrieve after persist before flush: " + ex.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Trade1.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Trade1(org.jpox.samples.versioned.Trade1) Date(java.util.Date) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException)

Example 5 with Trade1

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

the class OptimisticTest method testOptimisticQuery.

/**
 * Test of an optimistic query.
 * The query connection will be closed just after the query, so this tests
 * the access to the elements even though there is no active connection.
 */
public void testOptimisticQuery() {
    PersistenceManager pm1 = pmf.getPersistenceManager();
    Transaction tx1 = pm1.currentTransaction();
    try {
        // Create some data
        tx1.begin();
        Trade1 t1 = new Trade1("Tony Blair", 200.0, new Date());
        pm1.makePersistent(t1);
        Trade1 t2 = new Trade1("Osama Bin Laden", 50000.0, new Date());
        pm1.makePersistent(t2);
        Trade1 t3 = new Trade1("Jacques Chirac", 40000.0, new Date());
        pm1.makePersistent(t3);
        tx1.commit();
    } catch (Exception e) {
        LOG.error(e);
        fail("optimisticQueryTest failed while creating its reference data : " + e.getMessage());
    } finally {
        if (tx1.isActive()) {
            tx1.rollback();
        }
        pm1.close();
    }
    // Perform an optimistic query on this data
    PersistenceManager pm2 = pmf.getPersistenceManager();
    pm2.setIgnoreCache(true);
    Transaction tx2 = pm2.currentTransaction();
    tx2.setOptimistic(true);
    try {
        tx2.begin();
        Query q = pm2.newQuery(pm2.getExtent(Trade1.class, false));
        List results = (List) q.execute();
        Iterator resultsIter = results.iterator();
        while (resultsIter.hasNext()) {
            resultsIter.next();
        }
        tx2.commit();
    } catch (Exception e) {
        LOG.error(e);
        fail("optimisticQueryTest failed while executing the query : " + e.getMessage());
    } finally {
        if (tx2.isActive()) {
            tx2.rollback();
        }
        pm2.close();
        clean(Trade1.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Trade1(org.jpox.samples.versioned.Trade1) Iterator(java.util.Iterator) List(java.util.List) Date(java.util.Date) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)18 Transaction (javax.jdo.Transaction)18 Trade1 (org.jpox.samples.versioned.Trade1)18 Date (java.util.Date)17 JDOOptimisticVerificationException (javax.jdo.JDOOptimisticVerificationException)13 Query (javax.jdo.Query)4 List (java.util.List)3 Trade1Holder (org.jpox.samples.versioned.Trade1Holder)2 Iterator (java.util.Iterator)1 Properties (java.util.Properties)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1 DataStoreCache (javax.jdo.datastore.DataStoreCache)1 JDODataStoreCache (org.datanucleus.api.jdo.JDODataStoreCache)1 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)1