Search in sources :

Example 51 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory 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 52 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory 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 53 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.

the class AttachDetachReplicateTest method testMoveAcrossDatastoresWithRelation.

/**
 * Test of detaching from one datastore and persisting the objects to another.
 * As we copy a group of related objects JPOX should serialize SQLs in the
 * way that do not break foreign key constraints.
 */
public void testMoveAcrossDatastoresWithRelation() {
    PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
    try {
        PersistenceManager pm1 = pmf.getPersistenceManager();
        Detail detail = null;
        // Persist in first DB
        Transaction tx = null;
        try {
            tx = pm1.currentTransaction();
            tx.begin();
            // Create some dummy records, so we offset the identity values and make a valid test
            pm1.makePersistent(new Master());
            pm1.makePersistent(new Master());
            pm1.makePersistent(new Master());
            pm1.makePersistent(new Detail());
            pm1.makePersistent(new Detail());
            pm1.makePersistent(new Detail());
            pm1.makePersistent(new OtherDetail());
            pm1.makePersistent(new OtherDetail());
            pm1.makePersistent(new OtherDetail());
            // Create our test objects, Master with related Detail and OtherDetail
            Master master1 = new Master();
            master1.setId("master");
            Detail detail1 = new Detail();
            detail1.setId("detail");
            master1.addDetail(detail1);
            detail1.setMaster(master1);
            OtherDetail otherDetail1 = new OtherDetail();
            otherDetail1.setId("otherDetail1");
            master1.addOtherDetail(otherDetail1);
            otherDetail1.setMaster(master1);
            pm1.makePersistent(detail1);
            // Detach it for copying
            pm1.getFetchPlan().addGroup("all");
            pm1.getFetchPlan().setMaxFetchDepth(2);
            detail = (Detail) pm1.detachCopy(detail1);
            tx.commit();
        } catch (JDOUserException ue) {
            LOG.error(ue);
            fail("Exception thrown while creating object in first datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            pm1.close();
        }
        // Check our detached objects
        assertNotNull(detail.getMaster());
        assertEquals(detail.getMaster().getOtherDetails().size(), 1);
        // Copy to other DB
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        try {
            tx = pm2.currentTransaction();
            tx.begin();
            // Persist graph of three transient objects to see if this datastore works
            Master master2 = new Master();
            master2.setId("master2");
            Detail detail2 = new Detail();
            detail2.setId("detail2");
            master2.addDetail(detail2);
            detail2.setMaster(master2);
            OtherDetail otherDetail2 = new OtherDetail();
            otherDetail2.setId("otherDetail2");
            master2.addOtherDetail(otherDetail2);
            otherDetail2.setMaster(master2);
            pm2.makePersistent(detail2);
            // Replicate object graph of three detached objects from datastore 1
            pm2.makePersistent(detail);
            tx.commit();
        } catch (JDOUserException ue) {
            LOG.error(ue);
            fail("Exception thrown while copying object into second datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
        }
        // Check the persistence in the second datastore
        try {
            tx = pm2.currentTransaction();
            tx.begin();
            // Use Extent since PM may have just put object in cache.
            Extent e = pm2.getExtent(Master.class);
            Iterator iter = e.iterator();
            int noOfMasters = 0;
            while (iter.hasNext()) {
                noOfMasters++;
                iter.next();
            }
            assertTrue("Number of masters retrieved from second datastore is incorrect : was " + noOfMasters + " but should have been 2", noOfMasters == 2);
            e = pm2.getExtent(OtherDetail.class);
            iter = e.iterator();
            int noOfOtherDetails = 0;
            while (iter.hasNext()) {
                noOfOtherDetails++;
                iter.next();
            }
            assertTrue("Number of otherdetails retrieved from second datastore is incorrect : was " + noOfOtherDetails + " but should have been 2", noOfOtherDetails == 2);
            tx.commit();
        } catch (JDOUserException ue) {
            LOG.error(ue);
            fail("Exception thrown while querying object in second datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            pm2.close();
        }
    } finally {
        // Clean up our data in the main DB
        clean(Detail.class);
        clean(OtherDetail.class);
        clean(Master.class);
        // Clean up data in the other DB
        PersistenceManager pm = pmf2.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // delete all Detail objects
            tx.begin();
            Extent ext = pm.getExtent(Detail.class, true);
            Iterator it = ext.iterator();
            while (it.hasNext()) {
                Object o = it.next();
                pm.deletePersistent(o);
            }
            tx.commit();
            // delete all OtherDetail objects
            tx.begin();
            ext = pm.getExtent(OtherDetail.class, true);
            it = ext.iterator();
            while (it.hasNext()) {
                Object o = it.next();
                pm.deletePersistent(o);
            }
            tx.commit();
            // delete all Master objects
            tx.begin();
            ext = pm.getExtent(Master.class, true);
            it = ext.iterator();
            while (it.hasNext()) {
                Object o = it.next();
                pm.deletePersistent(o);
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    }
}
Also used : Master(org.datanucleus.samples.models.hashsetcollection.Master) OtherDetail(org.datanucleus.samples.models.hashsetcollection.OtherDetail) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOUserException(javax.jdo.JDOUserException) OtherDetail(org.datanucleus.samples.models.hashsetcollection.OtherDetail) Detail(org.datanucleus.samples.models.hashsetcollection.Detail)

Example 54 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.

the class SchemaTest method testColumnWidth.

/**
 * Test of the column width specification.
 * Test the PMF property "org.jpox.rdbms.stringLengthExceededAction".
 */
public void testColumnWidth() {
    try {
        // 1). Persist an object with a "serialNo" too long for the column, and expect an Exception
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee(245, "Fred", "Flintstone", "fred.flintstone@warnerbros.com", (float) 178.90, "123456789012345");
            pm.makePersistent(e);
            tx.commit();
            fail("Persisted an object with a field value that was too long for the column storing it!");
        } catch (JDOFatalUserException e) {
        // Expected this to be thrown
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // 2). Persist an object with a "serialNo" too long for the column, and use PMF option to truncate
        Properties userProps = new Properties();
        userProps.setProperty(RDBMSPropertyNames.PROPERTY_RDBMS_STRING_LENGTH_EXCEEDED_ACTION, "TRUNCATE");
        PersistenceManagerFactory pmf2 = getPMF(1, userProps);
        pm = pmf2.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee(245, "Fred", "Flintstone", "fred.flintstone@warnerbros.com", (float) 178.90, "123456789012345");
            pm.makePersistent(e);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown when persisting object with too-long String field but truncate selected");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf2.close();
    } finally {
        clean(Employee.class);
    }
}
Also used : JDOFatalUserException(javax.jdo.JDOFatalUserException) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) JDOFatalUserException(javax.jdo.JDOFatalUserException) JDOFatalInternalException(javax.jdo.JDOFatalInternalException) SQLException(java.sql.SQLException)

Example 55 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.

the class SchemaTest method testReadOnlyDatastore.

/**
 * Test of the Read-Only datastore facility.
 * Should prevent all attempts to write to the datastore.
 */
public void testReadOnlyDatastore() {
    try {
        // Create the necessary table and create a few objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            // Make sure our read-write PMF has schema for this class
            pm.getExtent(Developer.class);
            // Make sure our read-write PMF has schema for this class
            pm.getExtent(Manager.class);
            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_DATASTORE_READONLY, "true");
        PersistenceManagerFactory pmf2 = getPMF(1, userProps);
        assertTrue("The PMF should have had the ReadOnlyDatastore property, yet hasn't", getConfigurationForPMF(pmf2).getBooleanProperty(PropertyNames.PROPERTY_DATASTORE_READONLY));
        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, "1245C");
            pm2.makePersistent(e);
            tx2.commit();
            assertTrue("Should have thrown an exception when trying makePersistent on ReadOnly datastore", false);
        } catch (Exception e) {
            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();
            assertTrue("Should have thrown an exception when trying deletePersistent on ReadOnly datastore", false);
        } catch (Exception e) {
            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(23);
            }
            tx2.commit();
            assertTrue("Should have thrown an exception when modifying an object on ReadOnly datastore", false);
        } catch (Exception e) {
            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 ReadOnly 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)

Aggregations

PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)67 PersistenceManager (javax.jdo.PersistenceManager)52 Transaction (javax.jdo.Transaction)44 Properties (java.util.Properties)40 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)34 Employee (org.jpox.samples.models.company.Employee)18 Query (javax.jdo.Query)15 JDOUserException (javax.jdo.JDOUserException)13 Manager (org.jpox.samples.models.company.Manager)13 Iterator (java.util.Iterator)12 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)11 Extent (javax.jdo.Extent)10 JDODataStoreCache (org.datanucleus.api.jdo.JDODataStoreCache)10 JDOFatalUserException (javax.jdo.JDOFatalUserException)9 Collection (java.util.Collection)8 JDOException (javax.jdo.JDOException)8 DataStoreCache (javax.jdo.datastore.DataStoreCache)8 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)8 Level2Cache (org.datanucleus.cache.Level2Cache)8 SQLException (java.sql.SQLException)7