Search in sources :

Example 11 with LoginAccount

use of org.jpox.samples.one_one.unidir.LoginAccount 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 12 with LoginAccount

use of org.jpox.samples.one_one.unidir.LoginAccount 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

LoginAccount (org.jpox.samples.one_one.unidir.LoginAccount)12 PersistenceManager (javax.jdo.PersistenceManager)11 Transaction (javax.jdo.Transaction)10 Login (org.jpox.samples.one_one.unidir.Login)8 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)5 JDOUserException (javax.jdo.JDOUserException)3 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)2 JDOException (javax.jdo.JDOException)2 NamingException (javax.naming.NamingException)2 HeuristicMixedException (javax.transaction.HeuristicMixedException)2 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)2 NotSupportedException (javax.transaction.NotSupportedException)2 RollbackException (javax.transaction.RollbackException)2 SystemException (javax.transaction.SystemException)2 UserTransaction (javax.transaction.UserTransaction)2 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)2 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)2 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1