Search in sources :

Example 1 with JDOException

use of javax.jdo.JDOException in project motech by motech.

the class ComboboxDataMigrationHelper method migrateDataToSingleSelectTable.

private void migrateDataToSingleSelectTable(String dstTable, String field) throws DataMigrationFailedException {
    String srcTable = dstTable + "_" + field.toUpperCase();
    String mdsDataBase = mdsConfig.getDataDatabaseName();
    try {
        if (!sqlDBManager.hasColumn(mdsDataBase, dstTable, field)) {
            LOGGER.info(String.format("Adding column %s to table %s.", field, dstTable));
            executeQuery(prepareAddColumnQuery(dstTable, field));
        }
        LOGGER.info(String.format("Migrating data from table %s to table %s.", srcTable, dstTable));
        executeQuery(prepareMigrationToSingleSelectQuery(srcTable, dstTable, field));
    } catch (JDOException | SQLException e) {
        throw new DataMigrationFailedException(String.format("Error while migrating data to from %s to %s.", srcTable, dstTable), e);
    }
}
Also used : SQLException(java.sql.SQLException) DataMigrationFailedException(org.motechproject.mds.exception.entity.DataMigrationFailedException) JDOException(javax.jdo.JDOException)

Example 2 with JDOException

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

the class ViewTest method testViewOfSetWidgets.

public void testViewOfSetWidgets() throws Exception {
    /*
         * We can't run this test on Cloudscape because the view used by
         * SetWidgetCounts doesn't execute properly; some counts that should
         * be 0 come up 1.  This is presumably due to a bug in Cloudscape
         * (last tried on both 3.6 and 4.0).
         */
    if ("cloudscape".equals(vendorID)) {
        return;
    }
    try {
        LOG.info("Testing view derived from of " + StorageTester.TEST_OBJECT_COUNT + " " + SetWidget.class.getName() + " objects");
        tester.insertObjects(SetWidget.class);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent ext = pm.getExtent(SetWidgetCounts.class, true);
            Iterator exti = ext.iterator();
            int count = 0;
            while (exti.hasNext()) {
                SetWidgetCounts actual = (SetWidgetCounts) exti.next();
                SetWidgetCounts expected = new SetWidgetCounts(actual.getSetWidget());
                StorageTester.assertFieldsEqual(expected, actual);
                ++count;
            }
            tx.commit();
            assertEquals("Iteration over view extent returned wrong number of rows", StorageTester.TEST_OBJECT_COUNT, count);
            tx.begin();
            Query query = pm.newQuery(pm.getExtent(SetWidgetCounts.class, true));
            query.setFilter("normalSetSize != 0");
            query.setOrdering("sw.numElementWidgets descending");
            Collection results = (Collection) query.execute();
            TestObject[] objs = tester.getObjects();
            try {
                HashSet expected = new HashSet();
                for (int i = 0; i < objs.length; ++i) {
                    SetWidget sw = (SetWidget) objs[i];
                    if (sw.getNormalSet().size() != 0) {
                        expected.add(new SetWidgetCounts(sw));
                    }
                }
                assertTrue("Query has no expected results (test is broken)", !expected.isEmpty());
                assertTrue("Query returned no rows", !results.isEmpty());
                HashSet actual = new HashSet(results);
                assertEquals("Query returned duplicate rows", results.size(), actual.size());
                assertTrue("Query did not return expected results: expected " + expected + ", but was " + actual, TestObject.compareSet(expected, actual));
            } finally {
                query.closeAll();
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } catch (JDOException e) {
        LOG.error(">> Exception during test", e);
        fail("Exception occurred during test : " + e.getMessage());
    } finally {
        tester.removeObjects();
        clean(Widget.class);
    }
}
Also used : SetWidget(org.jpox.samples.widget.SetWidget) HashSetWidget(org.jpox.samples.widget.HashSetWidget) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) SetWidgetCounts(org.jpox.samples.rdbms.views.SetWidgetCounts) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection) HashSet(java.util.HashSet) JDOException(javax.jdo.JDOException)

Example 3 with JDOException

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

the class AttachDetachReplicateTest method testReplicateSimple2.

/**
 * Another test of replication, with no relations. Just the object
 */
public void testReplicateSimple2() {
    PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
    try {
        PersistenceManager pm1 = pmf.getPersistenceManager();
        JFireOrganisation organisation = null;
        JFireOrganisationID organisationID = JFireOrganisationID.create("datanucleus.jfire.org");
        // Persist in first DB
        Transaction tx = null;
        try {
            tx = pm1.currentTransaction();
            tx.begin();
            JFireOrganisation org1 = new JFireOrganisation(organisationID.organisationID);
            org1 = pm1.makePersistent(org1);
            // Detach it for copying
            organisation = pm1.detachCopy(org1);
            tx.commit();
        } catch (JDOException ue) {
            LOG.error("Exception thrown persisting/detaching object", ue);
            fail("Exception thrown while creating object in first datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            pm1.close();
        }
        // Check the detached object
        if (!JDOHelper.isDetached(organisation)) {
            fail("Organisation has not been detached!");
        }
        // Copy to other DB
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        tx = pm2.currentTransaction();
        try {
            tx.begin();
            pm2.makePersistent(organisation);
            tx.commit();
        } catch (JDOException ue) {
            LOG.error("Exception thrown replicating object", 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(JFireOrganisation.class);
            Iterator iter = e.iterator();
            boolean copied = false;
            while (iter.hasNext()) {
                JFireOrganisation o = (JFireOrganisation) iter.next();
                if (pm2.getObjectId(o).equals(organisationID)) {
                    copied = true;
                    break;
                }
            }
            assertTrue("Organisation was not copied to second datastore!", copied);
            tx.commit();
        } catch (JDOException ue) {
            LOG.error("Exception thrown checking results", ue);
            fail("Exception thrown while querying object in second datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            pm2.close();
        }
    } finally {
        PersistenceManagerFactory[] pmfs = new PersistenceManagerFactory[] { pmf, pmf2 };
        for (int i = 0; i < pmfs.length; ++i) {
            clean(pmf, JFireOrganisation.class);
        }
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JFireOrganisation(org.datanucleus.samples.jfire.organisation.JFireOrganisation) Extent(javax.jdo.Extent) JFireOrganisationID(org.datanucleus.samples.jfire.organisation.JFireOrganisationID) Iterator(java.util.Iterator) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOException(javax.jdo.JDOException)

Example 4 with JDOException

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

the class JDOQLResultTest method testResultWithIfElse.

/**
 * Test the use of result with "IF ... ELSE ...".
 */
public void testResultWithIfElse() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            BasicTypeHolder[] basics = new BasicTypeHolder[5];
            for (int i = 0; i < basics.length; i++) {
                basics[i] = new BasicTypeHolder();
                basics[i].setLongField(i + 1);
                basics[i].setShortField((short) (i + 11));
                basics[i].setCharField('0');
            }
            pm.makePersistentAll(basics);
            tx.commit();
            Object[] ids = new Object[5];
            for (int i = 0; i < basics.length; i++) {
                ids[i] = pm.getObjectId(basics[i]);
            }
            tx.begin();
            Query q = pm.newQuery(BasicTypeHolder.class);
            q.setResult("IF (this.longField > 2) 1 ELSE 0");
            Collection c = (Collection) q.execute();
            Assert.assertEquals(5, c.size());
            int numZeros = 0;
            int numOnes = 0;
            for (Object obj : c) {
                Long l = Long.valueOf("" + obj);
                if (l == 1) {
                    numOnes++;
                } else if (l == 0) {
                    numZeros++;
                }
            }
            assertEquals(3, numOnes);
            assertEquals(2, numZeros);
            tx.commit();
            tx.begin();
            try {
                // Test construct with no final "ELSE expr"
                q = pm.newQuery(BasicTypeHolder.class);
                q.setResult("IF (this.longField > 2) 1 ELSE IF (this.logField < 0) 0");
                q.compile();
                fail("Should have thrown exception with missing ELSE on IF construct, but didnt");
            } catch (JDOException jdoe) {
            // Expected
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(BasicTypeHolder.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) BasicTypeHolder(org.jpox.samples.types.basic.BasicTypeHolder) JDOException(javax.jdo.JDOException)

Example 5 with JDOException

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

the class PersistenceManagerFactoryTest method testClose.

/**
 * Test of the closure behaviour of the PMF.
 */
public void testClose() {
    // Create 10 to give a reasonable chance that the active tx ones aren't "closed" first internally
    PersistenceManager[] pms = new PersistenceManager[10];
    for (int i = 0; i < 10; ++i) {
        pms[i] = pmf.getPersistenceManager();
    }
    // Open and close a transaction on an arbitrary PM
    pms[3].currentTransaction().begin();
    pms[3].currentTransaction().commit();
    // Open (and leave active) a transaction on 2 arbitrary PMs
    pms[5].currentTransaction().begin();
    pms[7].currentTransaction().begin();
    // try to close PMF while has open tx
    boolean success = false;
    try {
        pmf.close();
    } catch (JDOException ex) {
        Throwable[] nested = ex.getNestedExceptions();
        assertNotNull("Expected nested exceptions", nested);
        assertEquals("Expected exactly 2 nested exceptions", 2, nested.length);
        for (PersistenceManager pm : pms) {
            assertFalse("None of the PMs should have been closed", pm.isClosed());
        }
        success = true;
        // Clean up the txns, PMs, and PMF
        pms[5].currentTransaction().commit();
        pms[7].currentTransaction().commit();
        pmf.close();
    } finally {
        getPMF();
    }
    pmf.getPersistenceManager();
    PersistenceManager pm3 = pmf.getPersistenceManager();
    pm3.currentTransaction().begin();
    pm3.currentTransaction().commit();
    success = false;
    try {
        pmf.close();
        success = true;
    } finally {
        getPMF();
    }
    assertTrue("should not have raised an exception", success);
    // test close twice
    pmf.close();
    try {
        pmf.close();
    } catch (JDOUserException e) {
        // JDO2.3 spec 11.4 allows redundant calls to close().
        fail("should not have raised an exception when closing a closed PMF");
    } finally {
        getPMF();
    }
    // Try to access a PM from the closed PMF
    pmf.close();
    try {
        pmf.getPersistenceManager();
        fail("should have raised an exception when accessing PM from a closed PMF");
    } catch (JDOUserException ue) {
    // Expected, as per section 11.4 of the JDO2 spec
    } catch (Exception e) {
        LOG.error("Incorrect exception : expected JDOUserException ", e);
        fail("wrong type of exception thrown when accessing PM from closed PMF " + e.getClass().getName());
    } finally {
        getPMF();
    }
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException) JDOException(javax.jdo.JDOException)

Aggregations

JDOException (javax.jdo.JDOException)32 PersistenceManager (javax.jdo.PersistenceManager)14 Transaction (javax.jdo.Transaction)13 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 Query (javax.jdo.Query)7 Iterator (java.util.Iterator)6 JDOUserException (javax.jdo.JDOUserException)6 NucleusException (org.datanucleus.exceptions.NucleusException)6 JDOUnsupportedOptionException (javax.jdo.JDOUnsupportedOptionException)5 Constructor (java.lang.reflect.Constructor)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 JDOFatalUserException (javax.jdo.JDOFatalUserException)4 PersistableExpression (javax.jdo.query.PersistableExpression)4 Collection (java.util.Collection)3 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)3 Person (org.jpox.samples.models.company.Person)3 Method (java.lang.reflect.Method)2 Date (java.sql.Date)2 SQLException (java.sql.SQLException)2