Search in sources :

Example 6 with JDOOptimisticVerificationException

use of javax.jdo.JDOOptimisticVerificationException in project tests by datanucleus.

the class OptimisticTest method testBasicVersionNumberStrategy.

/**
 * Test of conflicting transactions, using "version-number" strategy.
 */
public void testBasicVersionNumberStrategy() {
    PersistenceManager pm1 = pmf.getPersistenceManager();
    Transaction tx1 = pm1.currentTransaction();
    PersistenceManager pm2 = pmf.getPersistenceManager();
    Transaction tx2 = pm2.currentTransaction();
    try {
        tx1.begin();
        Trade1 t1 = new Trade1("Mr X", 100.0, new Date());
        pm1.makePersistent(t1);
        pm1.flush();
        assertNotNull("Object just persisted using makePersistent doesnt have a version!", JDOHelper.getVersion(t1));
        tx1.commit();
        Object id = pm1.getObjectId(t1);
        // retrieve the object in PM1/txn1
        pm1.setIgnoreCache(true);
        tx1.setOptimistic(true);
        tx1.begin();
        Trade1 t1a = (Trade1) pm1.getObjectById(id, true);
        // retrieve the object in PM2/txn2 (without the change from txn1 presumably)
        pm2.setIgnoreCache(true);
        tx2.setOptimistic(true);
        tx2.begin();
        Trade1 t1b = (Trade1) pm2.getObjectById(id, true);
        // update the object in PM1/txn1
        t1a.setPerson("Mr Y");
        // commit txn1 with the change
        tx1.commit();
        // Update in txn2
        t1b.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(Trade1.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Trade1(org.jpox.samples.versioned.Trade1) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) Date(java.util.Date) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException)

Example 7 with JDOOptimisticVerificationException

use of javax.jdo.JDOOptimisticVerificationException in project tests by datanucleus.

the class OptimisticTest method testBasicVersionNumberStrategyVersionFieldForSubclass.

/**
 * Test of conflicting transactions, using "version-number" strategy where the class has a version field.
 */
public void testBasicVersionNumberStrategyVersionFieldForSubclass() {
    PersistenceManager pm1 = pmf.getPersistenceManager();
    Transaction tx1 = pm1.currentTransaction();
    PersistenceManager pm2 = pmf.getPersistenceManager();
    Transaction tx2 = pm2.currentTransaction();
    try {
        tx1.begin();
        Trade4Sub tradeA = new Trade4Sub("Mr S", 200.0, new Date());
        tradeA.setSubName("SubName-1");
        pm1.makePersistent(tradeA);
        pm1.flush();
        assertEquals("Trade4Sub just persisted using makePersistent has incorrect version!", 1, tradeA.getVersion());
        tx1.commit();
        pmf.getDataStoreCache().evictAll();
        Object id = pm1.getObjectId(tradeA);
        // retrieve the object in PM1/txn1
        pm1.setIgnoreCache(true);
        tx1.setOptimistic(true);
        tx1.begin();
        Trade4Sub tradeB = (Trade4Sub) pm1.getObjectById(id, true);
        // retrieve the object in PM2/txn2 (without the change from txn1 presumably)
        pm2.setIgnoreCache(true);
        tx2.setOptimistic(true);
        tx2.begin();
        Trade4Sub tradeC = (Trade4Sub) pm2.getObjectById(id, true);
        // update the object in PM1/txn1
        tradeB.setSubName("SubName-2");
        // commit txn1 with the change
        tx1.commit();
        assertEquals("Version of Trade4Sub is incorrect", 2l, JDOHelper.getVersion(tradeB));
        // Update in txn2
        tradeC.setSubName("SubName-3");
        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) {
        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(Trade4Sub.class);
    }
}
Also used : Trade4Sub(org.jpox.samples.versioned.Trade4Sub) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) Date(java.util.Date) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException)

Example 8 with JDOOptimisticVerificationException

use of javax.jdo.JDOOptimisticVerificationException in project tests by datanucleus.

the class OptimisticTest method testConflictTransactionsVersionNumberFromQuery.

/**
 * Test of conflicting transactions, using "version-number" strategy, when the
 * object is retrieved via a query.
 */
public void testConflictTransactionsVersionNumberFromQuery() {
    PersistenceManager pm1 = pmf.getPersistenceManager();
    Transaction tx1 = pm1.currentTransaction();
    PersistenceManager pm2 = pmf.getPersistenceManager();
    Transaction tx2 = pm2.currentTransaction();
    try {
        // Persist the object
        tx1.begin();
        Trade1 t1 = new Trade1("Mr X", 100.0, new Date());
        pm1.makePersistent(t1);
        tx1.commit();
        // retrieve the object
        pm1.setIgnoreCache(true);
        tx1.setOptimistic(true);
        tx1.begin();
        Query q1 = pm1.newQuery(Trade1.class, "person == \"Mr X\"");
        List results = (List) q1.execute();
        t1 = (Trade1) results.get(0);
        assertNotNull("Optimistic version is null", JDOHelper.getVersion(t1));
        t1.setPerson("Mr Y");
        pm2.setIgnoreCache(true);
        tx2.setOptimistic(true);
        tx2.begin();
        Query q2 = pm2.newQuery(Trade1.class, "person == \"Mr X\"");
        results = (List) q2.execute();
        t1 = (Trade1) results.get(0);
        assertNotNull("Optimistic version is null", JDOHelper.getVersion(t1));
        t1.setPerson("Mr Z");
        // commit tx1
        tx1.commit();
        boolean success = false;
        try {
            tx2.commit();
        } catch (JDOOptimisticVerificationException ove) {
            success = true;
        }
        assertTrue("JDOOptimisticVerificationException expected", success);
        assertFalse("transaction should be rolledback", tx2.isActive());
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Exception thrown during test of conflictTransactions via query: " + ex.getMessage());
    } finally {
        if (tx1.isActive()) {
            tx1.rollback();
        }
        pm1.close();
        if (tx2.isActive()) {
            tx2.rollback();
        }
        pm2.close();
        // 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) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) List(java.util.List) Date(java.util.Date) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException)

Example 9 with JDOOptimisticVerificationException

use of javax.jdo.JDOOptimisticVerificationException in project tests by datanucleus.

the class OptimisticTest method testMultipleUpdates.

/**
 * Test two succeeding updates in the same transaction, with the
 * first updating the version in the datastore before the second is made and committed
 */
public void testMultipleUpdates() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Trade1 t1 = new Trade1("Mr Smith", 1400.0, new Date());
        pm.makePersistent(t1);
        tx.commit();
        Object id = pm.getObjectId(t1);
        // check conflict
        tx.setOptimistic(true);
        tx.begin();
        t1 = (Trade1) pm.getObjectById(id, true);
        t1.setPerson("Mr Jones");
        pm.flush();
        t1.setPerson("Mr Green");
        pm.flush();
        t1.setPerson("Mr Arbuthnot");
        tx.commit();
    // not seeing a JDOOptimisticVerificationException is the success here
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Exception thrown during test of conflictTransactions: " + 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)

Aggregations

JDOOptimisticVerificationException (javax.jdo.JDOOptimisticVerificationException)9 PersistenceManager (javax.jdo.PersistenceManager)9 Transaction (javax.jdo.Transaction)9 Date (java.util.Date)8 Trade1 (org.jpox.samples.versioned.Trade1)4 List (java.util.List)1 JDOException (javax.jdo.JDOException)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 Query (javax.jdo.Query)1 Organisation (org.jpox.samples.models.company.Organisation)1 Trade2 (org.jpox.samples.versioned.Trade2)1 Trade3 (org.jpox.samples.versioned.Trade3)1 Trade4 (org.jpox.samples.versioned.Trade4)1 Trade4Sub (org.jpox.samples.versioned.Trade4Sub)1