Search in sources :

Example 21 with Employee

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

the class CacheTest method clearEmployeeData.

protected void clearEmployeeData(PersistenceManagerFactory pmf) {
    Extent ext = null;
    java.util.Iterator it = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // disassociate all Employees and Departments from their Managers
        tx.begin();
        ext = pm.getExtent(Manager.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Manager mgr = (Manager) it.next();
            mgr.clearSubordinates();
            mgr.clearDepartments();
        }
        tx.commit();
        // delete all Employee objects
        tx.begin();
        ext = pm.getExtent(Employee.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Employee emp = (Employee) it.next();
            pm.deletePersistent(emp);
        }
        tx.commit();
        // delete all Qualification objects
        tx.begin();
        ext = pm.getExtent(Qualification.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Qualification q = (Qualification) it.next();
            pm.deletePersistent(q);
        }
        tx.commit();
        // delete all Department objects
        tx.begin();
        ext = pm.getExtent(Department.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Department d = (Department) it.next();
            pm.deletePersistent(d);
        }
        tx.commit();
        // delete all Manager objects
        tx.begin();
        ext = pm.getExtent(Manager.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Manager mgr = (Manager) it.next();
            pm.deletePersistent(mgr);
        }
        tx.commit();
        // delete all Person objects
        tx.begin();
        ext = pm.getExtent(Person.class, true);
        it = ext.iterator();
        while (it.hasNext()) {
            Person person = (Person) it.next();
            pm.deletePersistent(person);
        }
        tx.commit();
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Iterator(java.util.Iterator) Transaction(javax.jdo.Transaction) Extent(javax.jdo.Extent) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person)

Example 22 with Employee

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

the class CacheTest method performSerialNumberChange.

/**
 * Convenience method to update an object using a PM
 * @param pm The Persistence Manager
 * @param id Id of the object to update
 * @param newNumber New serial number to apply to the object
 */
private void performSerialNumberChange(PersistenceManager pm, Object id, String newNumber) {
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Employee empl = (Employee) pm.getObjectById(id, true);
        empl.setSerialNo(newNumber);
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Attempted to update a field on a cached object and the access failed : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 23 with Employee

use of org.jpox.samples.models.company.Employee 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();
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) 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)

Example 24 with Employee

use of org.jpox.samples.models.company.Employee 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);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) DataStoreCache(javax.jdo.datastore.DataStoreCache)

Example 25 with Employee

use of org.jpox.samples.models.company.Employee 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();
    }
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Level2Cache(org.datanucleus.cache.Level2Cache) 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) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Employee (org.jpox.samples.models.company.Employee)129 PersistenceManager (javax.jdo.PersistenceManager)126 Transaction (javax.jdo.Transaction)119 Query (javax.jdo.Query)68 JDOUserException (javax.jdo.JDOUserException)62 Manager (org.jpox.samples.models.company.Manager)46 List (java.util.List)44 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)37 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)24 Person (org.jpox.samples.models.company.Person)24 Collection (java.util.Collection)21 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)18 Iterator (java.util.Iterator)17 Properties (java.util.Properties)17 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)16 SQLException (java.sql.SQLException)12 StoreManager (org.datanucleus.store.StoreManager)12 Department (org.jpox.samples.models.company.Department)12 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)11