use of org.datanucleus.api.jdo.exceptions.TransactionNotReadableException in project datanucleus-api-jdo by datanucleus.
the class PersistentNontransactional method transitionReadField.
/**
* Method to transition to read-field state.
* @param op ObjectProvider.
* @param isLoaded if the field was previously loaded.
* @return new LifeCycle state.
*/
public LifeCycleState transitionReadField(ObjectProvider op, boolean isLoaded) {
Transaction tx = op.getExecutionContext().getTransaction();
if (!tx.isActive() && !tx.getNontransactionalRead()) {
throw new TransactionNotReadableException(Localiser.msg("027002"), op.getInternalObjectId());
}
if (tx.isActive() && !tx.getOptimistic()) {
// Save the fields for rollback.
op.saveFields();
op.refreshLoadedFields();
return changeState(op, P_CLEAN);
}
return this;
}
use of org.datanucleus.api.jdo.exceptions.TransactionNotReadableException in project tests by datanucleus.
the class PersistenceManagerTest method testNonTransactionReadNegative.
/**
* Check for non active transactions and non-transactional read is false.
*/
public void testNonTransactionReadNegative() {
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.setNontransactionalRead(false);
rex.getColor();
fail("Expected TransactionNotReadableException");
} catch (TransactionNotReadableException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.setNontransactionalRead(false);
Dog d = (Dog) pm.getObjectById(id, false);
d.getId();
} catch (TransactionNotReadableException ex) {
fail("Unexpected TransactionNotReadableException");
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.setNontransactionalRead(false);
Dog d = (Dog) pm.getObjectById(id);
d.getColor();
fail("Expected TransactionNotReadableException");
} catch (TransactionNotReadableException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.setNontransactionalRead(false);
pm.newQuery(Dog.class).execute();
fail("Expected TransactionNotReadableException");
} catch (TransactionNotReadableException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.setNontransactionalRead(false);
Iterator it = ((Collection) pm.newQuery(Dog.class).execute()).iterator();
Dog d = (Dog) it.next();
d.getColor();
fail("Expected TransactionNotReadableException");
} catch (TransactionNotReadableException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
pmf.getDataStoreCache().evictAll();
pm.evictAll();
try {
tx.setNontransactionalRead(false);
// Validation so needs to go to datastore
pm.getObjectById(id, true);
fail("Expected TransactionNotReadableException");
} catch (TransactionNotReadableException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.setNontransactionalRead(false);
Iterator it = ((Collection) pm.newQuery(Dog.class).execute()).iterator();
Dog d = (Dog) it.next();
d.getColor();
fail("Expected TransactionNotReadableException");
} catch (TransactionNotReadableException ex) {
// expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Dog.class);
}
}
Aggregations