Search in sources :

Example 91 with Manager

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

the class MultithreadPMTest method testMultipleDetachCopyAndFetchPlanModification.

public void testMultipleDetachCopyAndFetchPlanModification() {
    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];
        Runnable[] runner = new Runnable[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++) {
                if (i % 2 == 0) {
                    runner[i] = new MultithreadDetachRunner(pm, woody);
                } else {
                    runner[i] = new MultithreadFetchPlanRunner(pm);
                }
                threads[i] = new Thread(runner[i]);
                threads[i].start();
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].join();
                Exception e = null;
                if (runner[i] instanceof MultithreadDetachRunner) {
                    e = ((MultithreadDetachRunner) runner[i]).getException();
                } else if (runner[i] instanceof MultithreadFetchPlanRunner) {
                    e = ((MultithreadFetchPlanRunner) runner[i]).getException();
                }
                if (e != null) {
                    LOG.error("Exception during test", e);
                    fail("Exception thrown during test : " + e);
                }
            }
        } catch (Exception e) {
            fail(e.getMessage());
        } finally {
            if (pm.currentTransaction().isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(myPMF);
        myPMF.close();
    }
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) Employee(org.jpox.samples.models.company.Employee)

Example 92 with Manager

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

the class MultithreadPMTest method testMultipleNonTransactionalRead.

/**
 * Test changing the state
 */
public void testMultipleNonTransactionalRead() {
    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");
        woody.setManager(bart);
        pm.makePersistent(woody);
        pm.currentTransaction().commit();
        pm.currentTransaction().setNontransactionalRead(true);
        try {
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i] = new Thread(new Runnable() {

                    public void run() {
                        woody.getLastName();
                        woody.getManager().getLastName();
                    }
                });
            }
            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 93 with Manager

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

the class SQLQueryTest method testSelectStarQuery.

/**
 * Test of the use of SELECT * in a query.
 */
public void testSelectStarQuery() throws Exception {
    try {
        // Persist something to select
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager m1 = new Manager(1, "Barney", "Rubble", "barney.rubble@flintstones.com", 100, "123456");
            pm.makePersistent(m1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Do an SQL query to find the Manager
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            String sqlText = "SELECT * FROM MANAGER";
            Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
            query.setClass(Manager.class);
            List results = (List) query.execute();
            assertTrue("\"SELECT *\" query returned null, yet should have returned some results", results != null);
            assertEquals("Number of Manager objects retrieved from \"SELECT *\" query was incorrect", 1, results.size());
            Manager mgr = (Manager) results.iterator().next();
            // These will cause further SQL statements to retrieve the fields in the Person/Employee part of the object
            assertEquals("\"SELECT *\" query returned Manager with incorrect first name", "Barney", mgr.getFirstName());
            assertEquals("\"SELECT *\" query returned Manager with incorrect last name", "Rubble", mgr.getLastName());
            assertEquals("\"SELECT *\" query returned Manager with incorrect email", "barney.rubble@flintstones.com", mgr.getEmailAddress());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown from \"SELECT *\" query : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 94 with Manager

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

the class AttachDetachTest method testDetachAllOnCommitViaQuery.

/**
 * Test "DetachAllOnCommit" when we retrieve objects via query and commit the txn
 */
public void testDetachAllOnCommitViaQuery() {
    try {
        // Persist some objects
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@warnerbros.com", 125, "123409");
            Employee bugs = new Employee(2, "Bugs", "Bunny", "bugs@warnerbros.com", 200, "123410");
            Account bugsAcct = new Account();
            bugsAcct.setUsername("bugs");
            bugsAcct.setEnabled(true);
            bugs.setAccount(bugsAcct);
            Manager donald = new Manager(3, "Donald", "Duck", "donald@warnerbros.com", 400, "123400");
            donald.addSubordinate(woody);
            donald.addSubordinate(bugs);
            woody.setManager(donald);
            bugs.setManager(donald);
            pm.makePersistent(donald);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while persisting objects and committing transaction with detachAllOnCommit : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve Employees and check the detached states
        pm = newPM();
        pm.getFetchPlan().addGroup("groupSubordinates");
        pm.getFetchPlan().addGroup("groupA");
        pm.getFetchPlan().addGroup("groupC");
        pm.getFetchPlan().setMaxFetchDepth(3);
        ((JDOPersistenceManager) pm).setDetachAllOnCommit(true);
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(Employee.class);
            Collection results = (Collection) q.execute();
            // Detach all of Employees on commit
            tx.commit();
            assertThat((Collection<?>) results).as("Number of Employees retrieved is incorrect").hasSize(3);
            Iterator empIter = results.iterator();
            while (empIter.hasNext()) {
                Employee emp = (Employee) empIter.next();
                assertTrue("Employee " + StringUtils.toJVMIDString(emp) + " is not detached!", JDOHelper.isDetached(emp));
            }
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while retrieving objects with detachAllOnCommit : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) Account(org.jpox.samples.models.company.Account) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) 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) Iterator(java.util.Iterator) Collection(java.util.Collection) ClassWithNonPCCollection(org.datanucleus.samples.detach.ClassWithNonPCCollection) Manager(org.jpox.samples.models.company.Manager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 95 with Manager

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

the class AttachDetachTest method getDetachedManager.

/**
 * Helper method for giving a detached Manager object
 * @param id the manager object id
 * @param fetchPlanName the fetch plan
 * @return a detached Manager
 */
private Manager getDetachedManager(Object id, String fetchPlanName) {
    PersistenceManager pm = newPM();
    Transaction tx = pm.currentTransaction();
    pm.getFetchPlan().addGroup(fetchPlanName);
    Object detached = null;
    try {
        tx.begin();
        detached = pm.detachCopy(pm.getObjectById(id, true));
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception in test", e);
        fail(e.toString());
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
    return (Manager) detached;
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

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