Search in sources :

Example 11 with Qualification

use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.

the class ReachabilityTest method testOneToOneUniClassNewDeleted.

/**
 * Tests if reachability on commit does not try to persist an object that was reachable then deleted.
 * Uses 1-1 unidir relation.
 * Requires a JPOX extension so is outside the JDO2 spec.
 */
public void testOneToOneUniClassNewDeleted() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.setProperty(PropertyNames.PROPERTY_FLUSH_MODE, "manual");
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Organisation org = new Organisation("JPOX Corporation");
            assertTrue("Object state of Organisation is incorrect", !JDOHelper.isPersistent(org) && !JDOHelper.isNew(org) && !JDOHelper.isDirty(org));
            Qualification qual = new Qualification("ISO 4002 Certificate 17001");
            // persist Qualification
            pm.makePersistent(qual);
            // Relate the objects so persisting Organisation by reachability
            qual.setOrganisation(org);
            pm.flush();
            // Remove the reference to the organisation and delete the organisation
            qual.setOrganisation(null);
            // Adding pm.flush(); here will make it work
            pm.deletePersistent(org);
            // Clear references and wait so that the references are GCed
            org = null;
            qual = null;
            Thread.sleep(2000);
            // now commit, should not fail but can if it gets the updates in the wrong order (since is UNIDIRECTIONAL relation)
            tx.commit();
        } catch (Exception e) {
            // This can happen if the commit processes the delete of the object before the update of the owner
            e.printStackTrace();
            fail("Exception thrown when commiting deleted object which should not have affected commit");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clear out our data
        clean(Qualification.class);
        clean(Organisation.class);
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) Organisation(org.jpox.samples.models.company.Organisation) PersistenceManager(javax.jdo.PersistenceManager)

Example 12 with Qualification

use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.

the class ApplicationIdPersistenceTest method testInsertSerialised.

public void testInsertSerialised() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object id = null;
        try {
            tx.begin();
            Qualification q = new Qualification("MSc");
            Calendar cal = GregorianCalendar.getInstance();
            cal.set(Calendar.YEAR, 2010);
            cal.set(Calendar.MONTH, 5);
            cal.set(Calendar.DAY_OF_MONTH, 10);
            q.setDate(cal.getTime());
            pm.makePersistent(q);
            tx.commit();
            id = pm.getObjectId(q);
        } catch (Exception e) {
            LOG.error("Exception during persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        // Retrieve and check it
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Qualification q = (Qualification) pm.getObjectById(id);
            Date d = q.getDate();
            assertNotNull("Date is null!", d);
            Calendar cal = GregorianCalendar.getInstance();
            cal.setTime(d);
            assertEquals("Year is wrong", 2010, cal.get(Calendar.YEAR));
            assertEquals("Month is wrong", 5, cal.get(Calendar.MONTH));
            assertEquals("Day is wrong", 10, cal.get(Calendar.DAY_OF_MONTH));
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during retrieve", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Qualification.class);
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) JDOException(javax.jdo.JDOException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) Date(java.util.Date)

Example 13 with Qualification

use of org.jpox.samples.models.company.Qualification 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

PersistenceManager (javax.jdo.PersistenceManager)13 Transaction (javax.jdo.Transaction)13 Qualification (org.jpox.samples.models.company.Qualification)13 Organisation (org.jpox.samples.models.company.Organisation)6 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)4 Manager (org.jpox.samples.models.company.Manager)4 Collection (java.util.Collection)3 Properties (java.util.Properties)3 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)3 Query (javax.jdo.Query)3 DataStoreCache (javax.jdo.datastore.DataStoreCache)3 JDODataStoreCache (org.datanucleus.api.jdo.JDODataStoreCache)3 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)3 Department (org.jpox.samples.models.company.Department)3 Person (org.jpox.samples.models.company.Person)3 Calendar (java.util.Calendar)2 Date (java.util.Date)2 GregorianCalendar (java.util.GregorianCalendar)2 Level2Cache (org.datanucleus.cache.Level2Cache)2 StoreManager (org.datanucleus.store.StoreManager)2