use of org.jpox.samples.versioned.Trade1 in project tests by datanucleus.
the class OptimisticTest method testRefreshOfOptimisticObjects.
/**
* Test the use of refresh() on an object that has been updated in another PM.
* It should update the value in the object so it is up to date.
*/
public void testRefreshOfOptimisticObjects() {
try {
Object id1 = null;
// Create some data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Trade1 t1 = new Trade1("Fred Smith", 100.00, new Date());
pm.makePersistent(t1);
tx.commit();
id1 = pm.getObjectId(t1);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
PersistenceManager pm1 = pmf.getPersistenceManager();
Transaction tx1 = pm1.currentTransaction();
PersistenceManager pm2 = pmf.getPersistenceManager();
Transaction tx2 = pm2.currentTransaction();
try {
tx1.setOptimistic(true);
tx2.setOptimistic(true);
tx1.begin();
tx2.begin();
// Retrieve the data (in both PMs)
Trade1 t1pm1 = (Trade1) pm1.getObjectById(id1);
Trade1 t1pm2 = (Trade1) pm2.getObjectById(id1);
// Update it in pm1
t1pm1.setPerson("Fred Smithy");
tx1.commit();
// refresh t1pm2 so it's up to date
pm2.refresh(t1pm2);
assertEquals("Name of person in other pm has just been refreshed and name is incorrect", t1pm2.getPerson(), "Fred Smithy");
tx2.commit();
} catch (JDOOptimisticVerificationException e) {
fail("Exception thrown while refreshing optimistic object : " + e.getMessage());
} finally {
if (tx1.isActive()) {
tx1.rollback();
}
if (tx2.isActive()) {
tx2.rollback();
}
pm1.close();
pm2.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 testPCnewToPCdirty.
/**
* Test pc-new objects transiting to pc-dirty in optimistic tx.
*/
public void testPCnewToPCdirty() {
PersistenceManager pm1 = pmf.getPersistenceManager();
Transaction tx1 = pm1.currentTransaction();
try {
tx1.setOptimistic(true);
// ---
// test 1
// ---
tx1.begin();
Trade1 t1 = new Trade1("Mr X pc-new", 100.0, new Date());
pm1.makePersistent(t1);
pm1.flush();
pm1.getObjectId(t1);
t1.setPerson("Mr X pc-dirty");
tx1.commit();
// ---
// test 2
// ---
tx1.begin();
Trade1 t2 = new Trade1("Mr Y pc-new", 200.0, new Date());
pm1.makePersistent(t2);
tx1.commit();
Object id2 = pm1.getObjectId(t2);
tx1.begin();
Trade1 t2b = (Trade1) pm1.getObjectById(id2);
t2b.setPerson("Mr Y pc-dirty");
tx1.commit();
tx1.begin();
t2b.setPerson("Mr Y pc-dirty-1");
tx1.commit();
// ---
// test 3
// ---
tx1.begin();
Trade1 t3 = new Trade1("Mr Z pc-new", 300.0, new Date());
pm1.makePersistent(t3);
pm1.flush();
pm1.getObjectId(t3);
tx1.commit();
tx1.begin();
t3.setPerson("Mr Z pc-dirty");
tx1.commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (tx1.isActive()) {
tx1.rollback();
}
pm1.close();
// Clean out our data
clean(Trade1.class);
}
}
use of org.jpox.samples.versioned.Trade1 in project tests by datanucleus.
the class OptimisticTest method testDetachCopyVersionNumber.
/**
* Test of detachCopy and that the version number is stored when detaching.
*/
public void testDetachCopyVersionNumber() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
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));
// Detach a copy
trade = (Trade1) pm.detachCopy(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();
tx = pm.currentTransaction();
tx.setOptimistic(true);
try {
tx.begin();
trade = (Trade1) pm.makePersistent(trade);
pm.flush();
// Make sure version is set after flushing
assertEquals("Object version has not been updated correctly on attach", new Long(2), JDOHelper.getVersion(trade));
// Detach a copy
trade = (Trade1) pm.detachCopy(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();
tx = pm.currentTransaction();
tx.setOptimistic(true);
try {
tx.begin();
trade = (Trade1) pm.makePersistent(trade);
pm.flush();
// Make sure version is set after flushing
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 testDetachAttach.
/**
* tests the use of versions and attach/detach.
*/
public void testDetachAttach() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// create some data
tx.begin();
Trade1 t1 = new Trade1("Mr X", 250.0, new Date());
pm.makePersistent(t1);
tx.commit();
// retrieve the trade
tx.setOptimistic(true);
tx.begin();
Query query = pm.newQuery(Trade1.class);
t1 = (Trade1) ((Collection) query.execute()).iterator().next();
Long version = (Long) JDOHelper.getVersion(t1);
Trade1 detachedTrade1 = (Trade1) pm.detachCopy(t1);
Long detachedVersion = (Long) JDOHelper.getVersion(detachedTrade1);
assertEquals("Version of object and its detached form are not the same!", version, detachedVersion);
tx.commit();
// modify detached trade
detachedTrade1.setPerson("Mr Y");
// attach the trade
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.setOptimistic(true);
tx.begin();
pm.makePersistent(detachedTrade1);
tx.commit();
// retrieve the object and check the version
tx.setOptimistic(true);
tx.begin();
query = pm.newQuery(Trade1.class);
t1 = (Trade1) ((Collection) query.execute()).iterator().next();
Long latestVersion = (Long) JDOHelper.getVersion(t1);
assertEquals("Version of attached object has incorrect version", new Long(detachedVersion.longValue() + 1), latestVersion);
tx.commit();
} 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 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);
}
}
Aggregations