use of org.jpox.samples.versioned.Trade1 in project tests by datanucleus.
the class OptimisticTest method testDetachAllOnCommitVersionNumber.
/**
* Test of detachAllOnCommit and that the version number is stored when detaching.
*/
public void testDetachAllOnCommitVersionNumber() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
pm.setDetachAllOnCommit(true);
Transaction tx = pm.currentTransaction();
tx.setOptimistic(true);
Trade1 trade = null;
try {
tx.begin();
trade = new Trade1("Mr X", 100.0, new Date());
pm.makePersistent(trade);
pm.flush();
// Make sure version is set after flushing
assertNotNull("Object just persisted using makePersistent doesnt have a version!", JDOHelper.getVersion(trade));
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error("Exception thrown while persisting/detaching object", e);
fail("Exception thrown while persisting/detaching " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Update the object
assertEquals("Detached object has incorrect version", new Long(1), JDOHelper.getVersion(trade));
trade.setPerson("Mr Y");
// Attach the object
pm = pmf.getPersistenceManager();
pm.setDetachAllOnCommit(true);
tx = pm.currentTransaction();
tx.setOptimistic(true);
try {
tx.begin();
trade = (Trade1) pm.makePersistent(trade);
pm.flush();
assertEquals("Object version has not been updated correctly on attach", new Long(2), JDOHelper.getVersion(trade));
tx.commit();
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception thrown during test of conflictTransactions: " + ex.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Update the object
assertEquals("Detached object has incorrect version", new Long(2), JDOHelper.getVersion(trade));
trade.setPerson("Mr Z");
// Attach the object
pm = pmf.getPersistenceManager();
pm.setDetachAllOnCommit(true);
tx = pm.currentTransaction();
tx.setOptimistic(true);
try {
tx.begin();
trade = (Trade1) pm.makePersistent(trade);
pm.flush();
assertEquals("Object version has not been updated correctly on attach", new Long(3), JDOHelper.getVersion(trade));
tx.commit();
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception thrown during test of conflictTransactions: " + ex.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(Trade1.class);
}
}
use of org.jpox.samples.versioned.Trade1 in project tests by datanucleus.
the class OptimisticTest method testVersionResetOnRollback.
public void testVersionResetOnRollback() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
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();
Object id = pm.getObjectId(t1);
assertEquals("Version is incorrect", new Long(1), JDOHelper.getVersion(t1));
pmf.getDataStoreCache().evictAll();
// retrieve the object in PM1/txn1
pm.setIgnoreCache(true);
tx.setOptimistic(true);
tx.begin();
Trade1 t1a = (Trade1) pm.getObjectById(id, true);
// Update the object and flush to the datastore
t1a.setPerson("Mr Y");
pm.flush();
assertEquals("Version is incorrect", new Long(2), JDOHelper.getVersion(t1a));
// rollback the transaction
tx.rollback();
assertEquals("Version is incorrect", new Long(1), JDOHelper.getVersion(t1a));
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception thrown during test of rollback effect on version: " + ex.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
// Clean out our data
clean(Trade1.class);
}
}
use of org.jpox.samples.versioned.Trade1 in project tests by datanucleus.
the class CacheTest method testOptimisticTransactionWithL2Cache.
/**
* Test use of optimistic txns and L2 cache. Tests that we store and retrieve the version correctly
* when using L2 cached objects
*/
public void testOptimisticTransactionWithL2Cache() {
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
PersistenceManagerFactory cachePMF = getPMF(1, userProps);
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
// All Trade1 get pinned
l2Cache.pinAll(true, Trade1.class);
// Create some data we can use for access
PersistenceManager pm = cachePMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
Object version = null;
try {
tx.begin();
Trade1 trade = new Trade1("Woody Woodpecker", 500.0, new java.util.Date());
pm.makePersistent(trade);
// trade will be pinned since we have all Trade1 objects being pinned
tx.commit();
id = JDOHelper.getObjectId(trade);
version = JDOHelper.getVersion(trade);
assertNotNull("Version of persisted object is null!!", JDOHelper.getVersion(trade));
} catch (Exception e) {
e.printStackTrace();
fail("Error persisting basic data necessary to run optimistic L2 test");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Start new PM so we know "woody" isnt present in the L1 cache and has to get from L2
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Trade1 trade = (Trade1) pm.getObjectById(id);
assertNotNull("Version of retrieved L2 cached object is null!!", JDOHelper.getVersion(trade));
assertEquals("Versions of original/retrieved objects are different so wasn't L2 cached correctly", version, JDOHelper.getVersion(trade));
trade.setPerson("Donald Duck");
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error updating object that was retrieved from the L2 cache : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(cachePMF, Trade1.class);
cachePMF.close();
;
}
}
Aggregations