Search in sources :

Example 11 with Manager

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

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

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

Example 14 with Manager

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

the class JDOQLBasicTest method testSingleStringParameters.

/**
 * Test case to use JDOQL single-string parameters
 */
public void testSingleStringParameters() {
    try {
        Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
        Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        // Eh, what's up, doc?
        Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
        // Meep! Meep!
        Employee roadrunner = new Employee(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
        Manager bart2 = new Manager(5, "Bart", "Smith", "bart@smith.com", 4, "serial 5");
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            pm.makePersistent(bunny);
            pm.makePersistent(roadrunner);
            pm.makePersistent(bart2);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery("SELECT FROM " + Employee.class.getName() + " EXCLUDE SUBCLASSES WHERE firstName == christianname PARAMETERS java.lang.String christianname");
            List results = (List) q.execute("Bart");
            assertEquals("Query returned incorrect number of objects from single-string parameter query : was " + results.size() + " but should have been 1", 1, results.size());
            q.closeAll();
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) ArrayList(java.util.ArrayList) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 15 with Manager

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

the class JDOQLBasicTest method testKeepResultsQueryAfterTxClose.

/**
 * Test for the end of a transaction and the effect on query results.
 */
public void testKeepResultsQueryAfterTxClose() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
            Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 1, "serial 2");
            pm.makePersistent(homer);
            pm.makePersistent(bart);
            pm.flush();
            Query q = pm.newQuery(Manager.class);
            List results = (List) q.execute();
            assertFalse(results.isEmpty());
            results.iterator().next();
            tx.rollback();
            results.iterator().next();
        } catch (Exception e) {
            LOG.error("Exception during test", e);
            fail("Exception thrown when trying to access QueryResult after closing the Txn : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Manager.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) ArrayList(java.util.ArrayList) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)106 Manager (org.jpox.samples.models.company.Manager)106 Transaction (javax.jdo.Transaction)97 Department (org.jpox.samples.models.company.Department)50 Query (javax.jdo.Query)48 Employee (org.jpox.samples.models.company.Employee)45 JDOUserException (javax.jdo.JDOUserException)41 Collection (java.util.Collection)40 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)38 StoreManager (org.datanucleus.store.StoreManager)34 InsuranceDepartment (org.jpox.samples.models.company.InsuranceDepartment)29 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)27 Iterator (java.util.Iterator)22 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)17 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)15 JDOException (javax.jdo.JDOException)14 Extent (javax.jdo.Extent)13 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)13 List (java.util.List)11 Properties (java.util.Properties)10