Search in sources :

Example 16 with Level2Cache

use of org.datanucleus.cache.Level2Cache 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();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Level2Cache(org.datanucleus.cache.Level2Cache) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) DataStoreCache(javax.jdo.datastore.DataStoreCache) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

Level2Cache (org.datanucleus.cache.Level2Cache)16 Properties (java.util.Properties)8 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)8 PersistenceManager (javax.jdo.PersistenceManager)8 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)8 Transaction (javax.jdo.Transaction)8 JDODataStoreCache (org.datanucleus.api.jdo.JDODataStoreCache)8 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)8 DataStoreCache (javax.jdo.datastore.DataStoreCache)6 CachedPC (org.datanucleus.cache.CachedPC)6 ObjectProvider (org.datanucleus.state.ObjectProvider)4 Employee (org.jpox.samples.models.company.Employee)4 Manager (org.jpox.samples.models.company.Manager)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Query (javax.jdo.Query)2 CacheUniqueKey (org.datanucleus.cache.CacheUniqueKey)2 Persistable (org.datanucleus.enhancement.Persistable)2 IdentityReference (org.datanucleus.identity.IdentityReference)2 UniqueMetaData (org.datanucleus.metadata.UniqueMetaData)2