Search in sources :

Example 46 with Manager

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

the class MultithreadedTest method testQueryAndDetach.

/**
 * Test that populates the datastore, and then starts many threads querying and detaching the data
 * and trying to access the detached data (checking for undetached fields that should have been detached).
 */
public void testQueryAndDetach() {
    addClassesToSchema(new Class[] { Employee.class, Manager.class, Developer.class });
    try {
        // Persist some data
        LOG.debug(">> Persisting data");
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.setProperty(PropertyNames.PROPERTY_DETACH_ON_CLOSE, "true");
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager mgr = new Manager(1, "The", "Boss", "the.boss@datanucleus.com", 200000, "100000");
            pm.makePersistent(mgr);
            for (int i = 0; i < 100; i++) {
                Employee emp = new Employee(i + 2, "FirstName" + i, "LastName" + i, "first.last." + i + "@datanucleus.com", 100000 + i, "12345" + i);
                emp.setManager(mgr);
                mgr.addSubordinate(emp);
                pm.makePersistent(emp);
            }
            tx.commit();
        } catch (Throwable thr) {
            LOG.error("Exception persisting objects", thr);
            fail("Exception persisting data : " + thr.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        LOG.debug(">> Persisted data");
        // Verify the persistence
        pm = pmf.getPersistenceManager();
        pm.setProperty(PropertyNames.PROPERTY_DETACH_ON_CLOSE, "true");
        tx = pm.currentTransaction();
        try {
            tx.begin();
            LOG.debug(">> Querying Employees");
            Query<Employee> q = pm.newQuery(Employee.class);
            List<Employee> emps = q.executeList();
            for (Employee e : emps) {
                LOG.debug(">> emp=" + e + " e.mgr=" + e.getManager());
            }
            LOG.debug(">> Queried Employees");
            tx.commit();
        } catch (Throwable thr) {
            LOG.error("Exception checking objects", thr);
            fail("Exception checking data : " + thr.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Create the Threads
        int THREAD_SIZE = 500;
        final String[] threadErrors = new String[THREAD_SIZE];
        Thread[] threads = new Thread[THREAD_SIZE];
        for (int i = 0; i < THREAD_SIZE; i++) {
            final int threadNo = i;
            threads[i] = new Thread(new Runnable() {

                public void run() {
                    String errorMsg = processQueryAndDetach(true);
                    threadErrors[threadNo] = errorMsg;
                }
            });
        }
        // Run the Threads
        LOG.debug(">> Starting threads");
        for (int i = 0; i < THREAD_SIZE; i++) {
            threads[i].start();
        }
        for (int i = 0; i < THREAD_SIZE; i++) {
            try {
                threads[i].join();
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }
        }
        LOG.debug(">> Completed threads");
        // Process any errors and fail the test if any threads failed present
        for (String error : threadErrors) {
            if (error != null) {
                fail(error);
            }
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction)

Example 47 with Manager

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

the class MultithreadPMTest method testMultipleTransitionWrite.

/**
 * Test changing the state
 */
public void testMultipleTransitionWrite() {
    Properties multiProps = new Properties();
    multiProps.setProperty(PropertyNames.PROPERTY_MULTITHREADED, "true");
    PersistenceManagerFactory myPMF = getPMF(1, multiProps);
    try {
        int THREAD_SIZE = 1000;
        Thread[] threads = new Thread[THREAD_SIZE];
        PersistenceManager pm = myPMF.getPersistenceManager();
        pm.currentTransaction().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");
        final Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        woody.setManager(bart);
        pm.makePersistent(woody);
        pm.currentTransaction().commit();
        pm.currentTransaction().begin();
        try {
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i] = new Thread(new Runnable() {

                    public void run() {
                        woody.setLastName("name");
                        woody.setManager(boss);
                    }
                });
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].start();
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                try {
                    threads[i].join();
                } catch (InterruptedException e) {
                    fail(e.getMessage());
                }
            }
        } finally {
            if (pm.currentTransaction().isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(myPMF);
        myPMF.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 48 with Manager

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

the class MultithreadPMTest method testMultipleNonTransitionWrite.

/**
 * Test changing the state
 */
public void testMultipleNonTransitionWrite() {
    Properties multiProps = new Properties();
    multiProps.setProperty(PropertyNames.PROPERTY_MULTITHREADED, "true");
    PersistenceManagerFactory myPMF = getPMF(1, multiProps);
    try {
        int THREAD_SIZE = 1000;
        Thread[] threads = new Thread[THREAD_SIZE];
        PersistenceManager pm = myPMF.getPersistenceManager();
        pm.currentTransaction().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");
        final Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        woody.setManager(bart);
        pm.makePersistent(woody);
        pm.currentTransaction().commit();
        pm.currentTransaction().setNontransactionalWrite(true);
        try {
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i] = new Thread(new Runnable() {

                    public void run() {
                        woody.setLastName("name");
                        woody.setManager(boss);
                    }
                });
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].start();
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                try {
                    threads[i].join();
                } catch (InterruptedException e) {
                    fail(e.getMessage());
                }
            }
        } finally {
            if (pm.currentTransaction().isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(myPMF);
        myPMF.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 49 with Manager

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

the class MultithreadPMTest method testMultipleDetachCopy.

public void testMultipleDetachCopy() {
    Properties multiProps = new Properties();
    multiProps.setProperty(PropertyNames.PROPERTY_MULTITHREADED, "true");
    PersistenceManagerFactory myPMF = getPMF(1, multiProps);
    try {
        int THREAD_SIZE = 1000;
        Thread[] threads = new Thread[THREAD_SIZE];
        MultithreadDetachRunner[] runner = new MultithreadDetachRunner[THREAD_SIZE];
        PersistenceManager pm = myPMF.getPersistenceManager();
        pm.currentTransaction().begin();
        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.currentTransaction().commit();
        pm.currentTransaction().begin();
        try {
            for (int i = 0; i < THREAD_SIZE; i++) {
                runner[i] = new MultithreadDetachRunner(pm, woody);
                threads[i] = new Thread(runner[i]);
                threads[i].start();
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].join();
                if (runner[i].getException() != null) {
                    LOG.error("Exception during test", runner[i].getException());
                    fail("Exception thrown during test : " + runner[i].getException());
                }
            }
        } catch (Exception e) {
            fail(e.getMessage());
        } finally {
            if (pm.currentTransaction().isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(myPMF);
        myPMF.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 50 with Manager

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

the class JDOQLBasicTest method testCast.

public void testCast() {
    try {
        Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
        Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
        bart.addSubordinate(boss);
        bart.addSubordinate(boss2);
        Department deptB = new Department("DeptB");
        bart.addDepartment(deptB);
        Qualification q1 = new Qualification("q1");
        q1.setPerson(boss);
        Qualification q2 = new Qualification("q2");
        q2.setPerson(boss2);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(bart);
            pm.makePersistent(homer);
            pm.makePersistent(boss);
            pm.makePersistent(boss2);
            pm.makePersistent(q1);
            pm.makePersistent(q2);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Qualification.class);
            Collection c = q.filter("((Employee)person).serialNo == \"serial 3\"").imports("import org.jpox.samples.models.company.Employee").executeList();
            assertEquals(1, c.size());
            assertEquals("q1", ((Qualification) c.iterator().next()).getName());
            q = pm.newQuery(Qualification.class);
            c = q.filter("((Manager)person).serialNo == \"serial 4\"").imports("import org.jpox.samples.models.company.Employee").executeList();
            assertEquals(1, c.size());
            assertEquals("q2", ((Qualification) c.iterator().next()).getName());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager)

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