Search in sources :

Example 11 with DataStoreCache

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();
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) Organisation(org.jpox.samples.models.company.Organisation) PersistenceManager(javax.jdo.PersistenceManager) GregorianCalendar(java.util.GregorianCalendar) 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) Person(org.jpox.samples.models.company.Person) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

DataStoreCache (javax.jdo.datastore.DataStoreCache)11 PersistenceManager (javax.jdo.PersistenceManager)10 Transaction (javax.jdo.Transaction)10 JDODataStoreCache (org.datanucleus.api.jdo.JDODataStoreCache)9 Properties (java.util.Properties)8 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)8 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)8 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)8 Level2Cache (org.datanucleus.cache.Level2Cache)6 Employee (org.jpox.samples.models.company.Employee)5 Manager (org.jpox.samples.models.company.Manager)3 Qualification (org.jpox.samples.models.company.Qualification)3 Iterator (java.util.Iterator)2 Query (javax.jdo.Query)2 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 GregorianCalendar (java.util.GregorianCalendar)1 HashMap (java.util.HashMap)1 List (java.util.List)1