use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.
the class CacheTest method testDetachAllOnCommitWithoutL2.
/**
* Basic test for "detachAllOnCommit" WITHOUT L2 cache usage.
*/
public void testDetachAllOnCommitWithoutL2() {
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_DETACH_ALL_ON_COMMIT, "true");
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "none");
PersistenceManagerFactory cachePMF = getPMF(1, userProps);
runL2CacheDetachmentTestForPMF(cachePMF, 5);
}
use of javax.jdo.PersistenceManagerFactory 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();
;
}
}
use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.
the class CacheTest method testL1SoftRefL2.
/**
* Basic test for L1 Cache using "SoftRefCache" and L2 cache.
*/
public void testL1SoftRefL2() {
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "soft");
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
PersistenceManagerFactory cachePMF = getPMF(1, userProps);
runL2CacheTestForPMF(cachePMF);
}
use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.
the class CacheTest method testL2CachedObject.
/**
* Test for storage of an object in the L2 cache, and whether it has its fields populated.
*/
public void testL2CachedObject() {
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 we can use for access
PersistenceManager pm = cachePMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object woodyId = null;
Object woodlessId = null;
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
// All Employees/Managers get pinned
l2Cache.pinAll(true, Employee.class);
tx.begin();
final Employee woody = new Employee(1, "Woody", null, "woody@woodpecker.com", 13, "serial 1", new Integer(10));
final Employee woodless = new Employee(2, "Woodless", "Woodpecker", "woodless@woodpecker.com", 14, "serial 2", new Integer(11));
Manager bart = new Manager(3, "Bart", "Simpson", "bart@simpson.com", 3, "serial 3");
woody.setManager(bart);
pm.makePersistent(woody);
woody.setLastName("Woodpecker");
pm.makePersistent(woodless);
woodyId = pm.getObjectId(woody);
woodlessId = pm.getObjectId(woodless);
// Woody, Woodless, and Bart will all be pinned since we have all Employee objects being pinned
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error persisting basic data necessary to run multithread test");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
Level2Cache l2Cache = ((JDODataStoreCache) cachePMF.getDataStoreCache()).getLevel2Cache();
assertEquals("Incorrect number of pinned objects : should have been 3 but is " + l2Cache.getNumberOfPinnedObjects(), 3, l2Cache.getNumberOfPinnedObjects());
assertTrue("Level 2 Cache returned that it is empty yet should have pinned object(s)!", !l2Cache.isEmpty());
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Employee woody = (Employee) pm.getObjectById(woodyId, false);
assertTrue("Object retrieved from L2 cache was null", woody != null);
assertTrue("Field of object retrieved from L2 cache was null!", woody.getLastName() != null);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error encountered accessing object from L2 cache : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
l2Cache.evict(woodlessId);
assertEquals("Level 2 Cache returned that it has " + l2Cache.getNumberOfPinnedObjects() + " pinned objects, yet should have 2", 2, l2Cache.getNumberOfPinnedObjects());
// Clear the cache and check if the objects are released
l2Cache.evictAll();
assertTrue("Level 2 Cache returned that it is not empty yet we just cleared it!", l2Cache.isEmpty());
assertEquals("Level 2 Cache returned that it has " + l2Cache.getNumberOfPinnedObjects() + " pinned objects, yet we just cleared it!", 0, l2Cache.getNumberOfPinnedObjects());
assertEquals("Level 2 Cache returned that it has " + l2Cache.getNumberOfUnpinnedObjects() + " unpinned objects, yet we just cleared it!", 0, l2Cache.getNumberOfUnpinnedObjects());
} finally {
clearEmployeeData(cachePMF);
cachePMF.close();
}
}
use of javax.jdo.PersistenceManagerFactory 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