use of org.datanucleus.api.jdo.exceptions.TransactionNotWritableException in project datanucleus-api-jdo by datanucleus.
the class PersistentNontransactional method transitionWriteField.
/**
* Method to transition to write-field state.
* @param op ObjectProvider.
* @return new LifeCycle state.
*/
public LifeCycleState transitionWriteField(ObjectProvider op) {
Transaction tx = op.getExecutionContext().getTransaction();
if (!tx.isActive() && !tx.getNontransactionalWrite()) {
throw new TransactionNotWritableException(Localiser.msg("027001"), op.getInternalObjectId());
}
if (tx.isActive()) {
// Save the fields for rollback.
op.saveFields();
return changeState(op, P_DIRTY);
}
// Save the fields for rollback.
op.saveFields();
return changeState(op, P_NONTRANS_DIRTY);
}
use of org.datanucleus.api.jdo.exceptions.TransactionNotWritableException in project tests by datanucleus.
the class PersistenceManagerTest method testNonTransactionWriteNegative.
/**
* Check for non active transactions and non-transactional write is false.
*/
public void testNonTransactionWriteNegative() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Dog rex = new Dog();
rex.setName("rex");
rex.setId("rex" + new Random().nextInt());
rex.setColor("blue");
Object id = null;
try {
tx.begin();
pm.makePersistent(rex);
id = pm.getObjectId(rex);
tx.commit();
tx.setNontransactionalWrite(false);
// check updating field
rex.setColor("yellow");
fail("Expected TransactionNotWritableException");
} catch (TransactionNotWritableException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
// check updating application id field
try {
tx.setNontransactionalWrite(false);
Dog d = (Dog) pm.getObjectById(id, false);
d.setId("newid");
fail("Expected TransactionNotWritableException");
} catch (TransactionNotWritableException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.setNontransactionalWrite(false);
pm.newQuery(Dog.class).deletePersistentAll();
fail("Expected TransactionNotActiveException");
} catch (TransactionNotActiveException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.setNontransactionalWrite(false);
pm.newQuery(Dog.class).deletePersistentAll((Object[]) new String[] { "stupidarg" });
fail("Expected TransactionNotActiveException");
} catch (TransactionNotActiveException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.setNontransactionalWrite(false);
Iterator it = ((Collection) pm.newQuery(Dog.class).execute()).iterator();
Dog d = (Dog) it.next();
d.setId("newid");
fail("Expected TransactionNotActiveException");
} catch (TransactionNotActiveException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Dog.class);
}
}
Aggregations