use of javax.jdo.datastore.DataStoreCache in project tests by datanucleus.
the class CacheTest method testSCOAndPCReuse.
/**
* Demonstrate refreshing SCO and PC field values for a PC object returned from the L2 cache.
*/
public void testSCOAndPCReuse() {
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 {
// Create some data
PersistenceManager pm = cachePMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object qualId = null;
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
l2Cache.pinAll(true, Qualification.class);
l2Cache.pinAll(true, Organisation.class);
l2Cache.pinAll(true, Person.class);
tx.begin();
Person tweety = new Person(1, "Tweety", "Pie", "tweety.pie@warnerbros.com");
pm.makePersistent(tweety);
Organisation org = new Organisation("The Training Company");
pm.makePersistent(org);
Qualification qual = new Qualification("Certified JPOX Developer");
qual.setPerson(tweety);
qual.setOrganisation(org);
qual.setDate(new GregorianCalendar(2005, 8, 02).getTime());
pm.makePersistent(qual);
qualId = pm.getObjectId(qual);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error persisting basic data necessary to run SCO/PC field reuse test");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
/*
* OK, now we have a Person, an Organisation and a Qualification object in 2L cache.
* Previously getting the Qualification object from 2L cache cleared SCO
* and PC fields, so accessing the room/guest or any of the date
* fields resulted in SQL query, even though they are in the cache.
* Let's see if that changed. The following should cause no SQL
* execution at all.
*/
PersistenceManager pm2 = cachePMF.getPersistenceManager();
tx = pm2.currentTransaction();
try {
tx.begin();
Qualification qual = (Qualification) pm2.getObjectById(qualId);
// these are PC fields, their primary keys are kept even in cache
assertEquals("Person's first name is not what expected", qual.getPerson().getFirstName(), "Tweety");
assertEquals("Person's last name is not what expected", qual.getPerson().getLastName(), "Pie");
// this is a SCO field, value is kept even in cache
assertEquals("From date is not what expected", qual.getDate(), new GregorianCalendar(2005, 8, 02).getTime());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error using objects");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm2.close();
}
/*
* Now we unpin all objects and clean out the cache. Then we make
* sure, that in spite of the changes, it is still reloading fields,
* when they are not available
*/
PersistenceManager pm3 = cachePMF.getPersistenceManager();
tx = pm3.currentTransaction();
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
l2Cache.unpinAll(true, Qualification.class);
l2Cache.unpinAll(true, Person.class);
l2Cache.unpinAll(true, Organisation.class);
l2Cache.evictAll();
tx.begin();
Qualification qual = (Qualification) pm3.getObjectById(qualId);
assertEquals("Person's first name is not what expected", qual.getPerson().getFirstName(), "Tweety");
assertEquals("Person's last name is not what expected", qual.getPerson().getLastName(), "Pie");
assertEquals("From date is not what expected", qual.getDate(), new GregorianCalendar(2005, 8, 02).getTime());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error using objects");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm3.close();
}
} finally {
clean(cachePMF, Qualification.class);
clean(cachePMF, Organisation.class);
clean(cachePMF, Person.class);
cachePMF.close();
}
}
Aggregations