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);
}
}
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);
}
}
Aggregations