use of org.datanucleus.api.jdo.JDODataStoreCache in project tests by datanucleus.
the class CacheTest method testL2CacheAfterReadApplicationIdentity.
/**
* Test for storage of an object in the L2 cache from a query or from getObjectById.
*/
public void testL2CacheAfterReadApplicationIdentity() {
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();
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
l2Cache.pinAll(true, Vote.class);
tx.begin();
Vote vote1 = new Vote(1, "Vote 1");
pm.makePersistent(vote1);
Vote vote2 = new Vote(2, "Vote 2");
pm.makePersistent(vote2);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error persisting basic data necessary to run optimistic L2 test");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
Level2Cache l2Cache = ((JDODataStoreCache) cachePMF.getDataStoreCache()).getLevel2Cache();
assertEquals("Incorrect number of pinned objects", 2, l2Cache.getNumberOfPinnedObjects());
assertEquals("Incorrect number of unpinned objects", 0, l2Cache.getNumberOfUnpinnedObjects());
l2Cache.evictAll();
assertEquals("Incorrect number of pinned objects after evict", 0, l2Cache.getNumberOfPinnedObjects());
assertEquals("Incorrect number of unpinned objects after evict", 0, l2Cache.getNumberOfUnpinnedObjects());
// Try getObjectById
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Vote vote1 = pm.getObjectById(Vote.class, 1);
Vote vote2 = pm.getObjectById(Vote.class, 2);
assertNotNull(vote1);
assertNotNull(vote2);
assertEquals("Incorrect number of pinned objects after getObjectById", 2, l2Cache.getNumberOfPinnedObjects());
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();
}
assertEquals("Incorrect number of pinned objects", 2, l2Cache.getNumberOfPinnedObjects());
assertEquals("Incorrect number of unpinned objects", 0, l2Cache.getNumberOfUnpinnedObjects());
l2Cache.evictAll();
assertEquals("Incorrect number of pinned objects after evict", 0, l2Cache.getNumberOfPinnedObjects());
assertEquals("Incorrect number of unpinned objects after evict", 0, l2Cache.getNumberOfUnpinnedObjects());
// Try Query
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query query = pm.newQuery(Vote.class);
Collection votes = (Collection) query.execute();
Iterator iter = votes.iterator();
while (iter.hasNext()) {
iter.next();
}
assertEquals("Incorrect number of pinned objects after Query", 2, l2Cache.getNumberOfPinnedObjects());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error retrieving object from the L2 cache : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(cachePMF, Vote.class);
cachePMF.close();
}
}
use of org.datanucleus.api.jdo.JDODataStoreCache in project tests by datanucleus.
the class CacheTest method testL2LoadedFields.
/**
* Test to check the retrieval of an object from the L2 cache and the observance of its loaded fields.
*/
public void testL2LoadedFields() {
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);
try {
// Create a PM and add an object
Object id = null;
PersistenceManager pm = cachePMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Person p1 = new Person(102, "George", "Bush", "george.bush@whitehouse.gov");
pm.makePersistent(p1);
id = pm.getObjectId(p1);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Clear the L2 cache so we dont have this object
Level2Cache l2Cache = ((JDODataStoreCache) cachePMF.getDataStoreCache()).getLevel2Cache();
l2Cache.evictAll();
// Pin all Person objects
l2Cache.pinAll(Person.class, false);
// Retrieve the object with just some fields
PersistenceManager pm1 = cachePMF.getPersistenceManager();
tx = pm1.currentTransaction();
pm1.getFetchPlan().setGroup("groupA");
try {
// Load the Person object - will only have firstName, lastName loaded.
// Will be added to L2 cache, so pin it
tx.begin();
pm1.getObjectById(id);
// George Bush will now be pinned since all Person objects are pinned
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving object to store in L2 cache with only few fields : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
// Object should now be in L2 cache
// Retrieve the object with all fields and check an unretrieved field
PersistenceManager pm2 = cachePMF.getPersistenceManager();
tx = pm2.currentTransaction();
try {
tx.begin();
Person p1 = (Person) pm2.getObjectById(id);
assertTrue("Additional field of L2 cached object hasn't been retrieved correctly (wasnt in original FetchPlan)", p1.getEmailAddress() != null);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving object from L2 cache : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
pm1.close();
pm2.close();
} finally {
// Clean out created data
clean(cachePMF, Person.class);
cachePMF.close();
}
}
use of org.datanucleus.api.jdo.JDODataStoreCache 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();
}
}
Aggregations