Search in sources :

Example 31 with JDOException

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

the class TransactionTest method testAutomaticRollback.

/**
 * Test for proper functioning of automatic rollback upon failed commit
 */
public void testAutomaticRollback() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_FOREIGN_KEYS)) {
        // No foreign keys with this datastore so cannot pass this test
        return;
    }
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setOptimistic(true);
        LoginAccount acct = new LoginAccount("Fred", "Flintstone", "fred", "yabbadabbadoo");
        Login login = acct.getLogin();
        try {
            tx.begin();
            pm.makePersistent(acct);
            tx.commit();
            // provoke FK violation
            tx.begin();
            pm.deletePersistent(login);
            boolean exceptionCaught = false;
            try {
                tx.commit();
                assertTrue("Should have caught exception during commit due to FK violation", false);
            } catch (JDOException e) {
                // Expected
                exceptionCaught = true;
            }
            assertTrue("No exception was thrown during commit so couldnt test autoRollback", exceptionCaught);
            // now verify that we can still commit, i.e. tx was rolled back properly and is not active anymore
            tx.begin();
            LoginAccount acct2 = new LoginAccount("Wilma", "Flintstone", "wilma", "pebbles");
            pm.makePersistent(acct2);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
    } finally {
        clean(LoginAccount.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Login(org.jpox.samples.one_one.unidir.Login) JDOException(javax.jdo.JDOException)

Example 32 with JDOException

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

the class TransactionTest method testSqlExceptionIsAccessible.

/**
 * Test that upon exception during commit, any SQLException that was the cause of the problem
 * is provided as nested exception
 */
public void testSqlExceptionIsAccessible() {
    if (vendorID == null) {
        // Only applicable to RDBMS
        return;
    }
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setOptimistic(true);
        LoginAccount acct = new LoginAccount("Fred", "Flintstone", "fred", "yabbadabbadoo");
        Login login = acct.getLogin();
        try {
            tx.begin();
            pm.makePersistent(acct);
            tx.commit();
            // provoke FK violation
            tx.begin();
            pm.deletePersistent(login);
            boolean exceptionCaught = false;
            try {
                tx.commit();
                assertTrue("Should have caught exception during commit due to FK violation", false);
            } catch (JDOException e) {
                Throwable nested = e.getCause();
                if (nested != null && (nested instanceof SQLException)) {
                    exceptionCaught = true;
                }
            }
            assertTrue("Did not catch JDOException with nested SQLException, although expected", exceptionCaught);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
    } finally {
        clean(LoginAccount.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) SQLException(java.sql.SQLException) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Login(org.jpox.samples.one_one.unidir.Login) 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