Search in sources :

Example 26 with Employee

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

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

the class SchemaTest method testFixedDatastore.

/**
 * Test of the fixed datastore facility.
 * Should prevent all attempts to change tables in the datastore, yet allow insert/delete of rows.
 */
public void testFixedDatastore() {
    try {
        // Create the necessary table and create a few objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee(123, "Barney", "Rubble", "barney.rubble@warnerbros.com", (float) 123.45, "1245C");
            pm.makePersistent(e);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Create a PMF for our read-only schema
        Properties userProps = new Properties();
        userProps.setProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES, "false");
        PersistenceManagerFactory pmf2 = getPMF(1, userProps);
        assertFalse("The PMF should have had the AutoCreate property as false, yet hasn't", getConfigurationForPMF(pmf2).getBooleanProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES));
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        // a). Try makePersistent
        Transaction tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Employee e = new Employee(123, "Barney", "Rubble", "barney.rubble@warnerbros.com", (float) 123.45, "1245D");
            pm2.makePersistent(e);
            tx2.commit();
        } catch (Exception e) {
            assertTrue("Should not have thrown an exception when trying makePersistent on fixed datastore", false);
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        // b). Try deletePersistent
        tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Extent ex = pm2.getExtent(Employee.class, true);
            Iterator iter = ex.iterator();
            while (iter.hasNext()) {
                Employee e = (Employee) iter.next();
                pm2.deletePersistent(e);
            }
            tx2.commit();
        } catch (Exception e) {
            assertTrue("Should not have thrown an exception when trying deletePersistent on fixed datastore", false);
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        // c). Try update
        tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Extent ex = pm2.getExtent(Employee.class, true);
            Iterator iter = ex.iterator();
            while (iter.hasNext()) {
                Employee e = (Employee) iter.next();
                e.setAge(21);
            }
            tx2.commit();
        } catch (Exception e) {
            assertTrue("Should not have thrown an exception when modifying an object on fixed datastore", false);
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        // d). Try query
        tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Query q = pm2.newQuery(Employee.class);
            Collection results = (Collection) q.execute();
            Iterator resultsIter = results.iterator();
            while (resultsIter.hasNext()) {
                resultsIter.next();
            }
            tx2.commit();
        } catch (Exception e) {
            assertTrue("Should have been able to access objects on a fixed datastore", false);
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        pm2.close();
        pmf2.close();
    } finally {
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) Collection(java.util.Collection) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) JDOFatalUserException(javax.jdo.JDOFatalUserException) JDOFatalInternalException(javax.jdo.JDOFatalInternalException) SQLException(java.sql.SQLException)

Example 28 with Employee

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

the class SchemaTest method testDefaultedFields.

/**
 * Test of the specification and persistence of a class with defaulted fields.
 */
public void testDefaultedFields() {
    addClassesToSchema(new Class[] { Employee.class });
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object id = null;
        try {
            tx.begin();
            Employee e = new Employee(245, "Fred", "Flintstone", "fred.flintstone@warnerbros.com", (float) 178.90, "1245C");
            pm.makePersistent(e);
            tx.commit();
            id = pm.getObjectId(e);
        } catch (Exception e) {
            LOG.error(e);
            fail("Persistence of object with defaulted fields failed when should have just used defaults : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Avoid L2 cache interference if enabled
        pmf.getDataStoreCache().evictAll(false, Employee.class);
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = (Employee) pm.getObjectById(id);
            assertEquals("Defaulted SalaryCurrency is incorrect", "GBP", e.getSalaryCurrency());
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            fail("Checking of object with defaulted fields failed : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDOFatalUserException(javax.jdo.JDOFatalUserException) JDOFatalInternalException(javax.jdo.JDOFatalInternalException) SQLException(java.sql.SQLException)

Example 29 with Employee

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

the class SchemaTest method testUnique.

/**
 * Test of the "unique" constraint creation. This uses the constraint on
 * a field (column) and tries to insert non-unique values.
 */
public void testUnique() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee(123, "Barney", "Rubble", "barney.rubble@warnerbros.com", (float) 123.45, "1245C");
            pm.makePersistent(e);
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            fail("Error persisting an Employee object : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        // Try to insert another Employee with the same serial number
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee(245, "Fred", "Flintstone", "fred.flintstone@warnerbros.com", (float) 178.90, "1245C");
            pm.makePersistent(e);
            tx.commit();
            fail("Was able to persist a second object with the same value as an existing record in a unique constraint!");
        } catch (Exception e) {
            LOG.info(e);
        // Expected to come here with a duplicate key exception
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDOFatalUserException(javax.jdo.JDOFatalUserException) JDOFatalInternalException(javax.jdo.JDOFatalInternalException) SQLException(java.sql.SQLException)

Example 30 with Employee

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

the class SchemaTest method testUnique.

/**
 * Test of the "unique" constraint creation. This uses the constraint on
 * a field (column) and tries to insert non-unique values.
 */
public void testUnique() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee(123, "Barney", "Rubble", "barney.rubble@warnerbros.com", (float) 123.45, "1245C");
            pm.makePersistent(e);
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            fail("Error persisting an Employee object : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        // Try to insert another Employee with the same serial number
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee(245, "Fred", "Flintstone", "fred.flintstone@warnerbros.com", (float) 178.90, "1245C");
            pm.makePersistent(e);
            tx.commit();
            fail("Was able to persist a second object with the same value as an existing record in a unique constraint!");
        } catch (Exception e) {
            LOG.info(e);
        // Expected to come here with a duplicate key exception
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException) SQLException(java.sql.SQLException) JDODataStoreException(javax.jdo.JDODataStoreException)

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