use of javax.jdo.datastore.DataStoreCache in project tests by datanucleus.
the class CacheTest method testEvictAll.
/**
* Test DataStoreCache.evictAll(Class, boolean)
*/
public void testEvictAll() {
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "soft");
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();
// 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);
// Woody, Woodless, and Bart will all be pinned since we have all Employee objects being pinned
// create a few unpinned objects so DefaultLevel2Cache.evictAll() will have something to iterate
// and remove a few times
Qualification quali = new Qualification("patience");
pm.makePersistent(quali);
quali = new Qualification("endurance");
pm.makePersistent(quali);
quali = new Qualification("humour");
pm.makePersistent(quali);
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();
// cannot assert reliably existence of unpinned objects as they can get GC'ed any time
// just check that the following executes without errors and that there are no unpinned objects
// afterwards
l2Cache.evictAll(Qualification.class, true);
assertTrue("Level 2 Cache returned that it has " + l2Cache.getNumberOfUnpinnedObjects() + " unpinned objects, yet we just cleared it!", l2Cache.getNumberOfUnpinnedObjects() == 0);
// check whether it was only the Qualification objects that got evicted
assertTrue("Incorrect number of pinned objects : should have been 3 but is " + l2Cache.getNumberOfPinnedObjects(), l2Cache.getNumberOfPinnedObjects() == 3);
assertTrue("Level 2 Cache returned that it is empty yet should have pinned object(s)!", !l2Cache.isEmpty());
// evict all Employee + subclass objects and check if the objects are released
l2Cache.evictAll(Employee.class, true);
assertTrue("Level 2 Cache returned that it is not empty yet we just cleared it!", l2Cache.isEmpty());
assertTrue("Level 2 Cache returned that it has " + l2Cache.getNumberOfPinnedObjects() + " pinned objects, yet we just cleared it!", l2Cache.getNumberOfPinnedObjects() == 0);
assertTrue("Level 2 Cache returned that it has " + l2Cache.getNumberOfUnpinnedObjects() + " unpinned objects, yet we just cleared it!", l2Cache.getNumberOfUnpinnedObjects() == 0);
} finally {
clearEmployeeData(cachePMF);
cachePMF.close();
}
}
use of javax.jdo.datastore.DataStoreCache in project tests by datanucleus.
the class CacheTest method testClassNotCacheable.
/**
* Test for whether a class that is marked as not cacheable is L2 cached.
*/
public void testClassNotCacheable() {
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);
tx.begin();
Qualification qual = new Qualification("Certified JPOX Developer");
pm.makePersistent(qual);
qualId = pm.getObjectId(qual);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error persisting data for test");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
JDODataStoreCache jdoCache = (JDODataStoreCache) cachePMF.getDataStoreCache();
Level2Cache l2Cache = jdoCache.getLevel2Cache();
assertNull("Qualification object should not have been L2 cached but was!", l2Cache.get(qualId));
// Try to retrieve this object - need a way of detecting if it tried the L2 cache
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
LOG.info(">> getObjectById qualId=" + qualId);
pm.getObjectById(qualId);
LOG.info(">> getObjectById qualId done");
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error persisting data for test");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(cachePMF, Qualification.class);
cachePMF.close();
}
}
use of javax.jdo.datastore.DataStoreCache in project tests by datanucleus.
the class CacheTest method runL2CacheDetachmentTestForPMF.
/**
* Method to perform a basic test of L2 Caching for the specified PMF.
* @param cachePMF The PMF to use
*/
private void runL2CacheDetachmentTestForPMF(final PersistenceManagerFactory cachePMF, final int loops) {
try {
// Create a PM and add an object
PersistenceManager pm = cachePMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
l2Cache.pinAll(true, Employee.class);
tx.begin();
Employee empl = new Employee(101, "Clint", "Eastwood", "clint.eastwood@hollywood.com", 1000000, "1234567");
pm.makePersistent(empl);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Test object state of instance multiple times; using (auto) detachment.
List<Employee> employees;
for (int i = 0; i < loops; i++) {
Collection<Employee> data = null;
// Retrieve and detach the object (detachAllAtCommit)
pm = cachePMF.getPersistenceManager();
pm.setDetachAllOnCommit(true);
tx = pm.currentTransaction();
try {
tx.begin();
Query<Employee> query = pm.newQuery(Employee.class);
data = query.executeList();
employees = new ArrayList<>();
employees.addAll(data);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check that it is detached
Iterator<Employee> iter = employees.iterator();
while (iter.hasNext()) {
Employee employee = (Employee) iter.next();
assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(employee));
}
}
} finally {
clean(Employee.class);
}
}
use of javax.jdo.datastore.DataStoreCache in project tests by datanucleus.
the class CacheTest method testL2CacheAfterReadDatastoreIdentity.
/**
* Test for storage of an object in the L2 cache from a query or from getObjectById.
*/
public void testL2CacheAfterReadDatastoreIdentity() {
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;
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
// All Employees/Managers get pinned
l2Cache.pinAll(true, Employee.class);
tx.begin();
final Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
pm.makePersistent(woody);
woodyId = pm.getObjectId(woody);
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", 1, 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());
// Do the query - should load the object into the L2 cache
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(Employee.class);
List results = (List) q.execute();
assertEquals("Number of objects returned by query was incorrect", 1, results.size());
Iterator iter = results.iterator();
while (iter.hasNext()) {
iter.next();
}
// Check the cache
assertEquals("L2 cache size is incorrect after query", 1, l2Cache.getSize());
assertTrue("Level 2 Cache (after query) doesnt have the object but should!", l2Cache.containsOid(woodyId));
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();
}
// Check/reset L2 cache
assertEquals("Incorrect number of pinned objects", 1, 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());
// Do getObjectById - should load the object into the L2 cache
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Employee woody = (Employee) pm.getObjectById(woodyId);
assertNotNull("Retrieved object is null!", woody);
// Check the cache
assertEquals("L2 cache size is incorrect (after getObjectById)", 1, l2Cache.getSize());
assertTrue("Level 2 Cache (after getObjectById) doesnt have the object but should!", l2Cache.containsOid(woodyId));
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();
}
} finally {
clearEmployeeData(cachePMF);
cachePMF.close();
}
}
use of javax.jdo.datastore.DataStoreCache in project tests by datanucleus.
the class CacheTest method testMultithreadObjectRead.
// ---------------------- Multithreaded tests -----------------------------
/**
* Test for the retrieval of an object from multiple threads where an L2
* cache is in use. All threads should find the object in the (L2) cache and
* return it.
*/
public void testMultithreadObjectRead() {
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;
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
l2Cache.pinAll(true, Employee.class);
tx.begin();
final Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
woody.setManager(bart);
pm.makePersistent(woody);
pm.makePersistent(bart);
woodyId = pm.getObjectId(woody);
// Woody/Bart will now be pinned since they we have all Employee/Manager 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();
assertTrue("Incorrect number of pinned objects : should have been 2 but is " + l2Cache.getNumberOfPinnedObjects(), l2Cache.getNumberOfPinnedObjects() == 2);
// Start multiple threads to retrieve the object
// All should find it in the L2 cache
int THREAD_SIZE = 5;
final Object objectId = woodyId;
Thread[] threads = new Thread[THREAD_SIZE];
try {
for (int i = 0; i < THREAD_SIZE; i++) {
final int threadNo = i;
threads[i] = new Thread(new Runnable() {
public void run() {
boolean success = true;
PersistenceManager pmthread = cachePMF.getPersistenceManager();
Transaction txthread = pmthread.currentTransaction();
try {
txthread.begin();
Employee woody = (Employee) pmthread.getObjectById(objectId);
if (woody == null) {
LOG.error("Object retrieved from L2 cache is null, but should have a value !");
success = false;
}
if (success) {
if (woody.getLastName() == null) {
LOG.error("Field of object retrieved from L2 cache is null, but should have its value !");
success = false;
}
if (success) {
if (woody.getManager().getLastName() == null) {
LOG.error("Field of related object retrieved from L2 cache is null, but should have a value !");
success = false;
}
}
}
txthread.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while accessing object in thread " + threadNo + " : " + e.getMessage());
} finally {
if (txthread.isActive()) {
txthread.rollback();
}
}
if (!success) {
fail("Thread had an error in retrieving the L2 cache objects. Inspect the log for the errors");
}
}
});
}
// Start the threads
for (int i = 0; i < THREAD_SIZE; i++) {
threads[i].start();
}
// Wait for the end of the threads
for (int i = 0; i < THREAD_SIZE; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
}
} catch (Exception e) {
fail("Error encountered while accessing the objects via the L2 Cache : " + e.getMessage());
} finally {
}
} finally {
clearEmployeeData(cachePMF);
cachePMF.close();
}
}
Aggregations