Search in sources :

Example 1 with TransactionNotReadableException

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;
}
Also used : Transaction(org.datanucleus.Transaction) TransactionNotReadableException(org.datanucleus.api.jdo.exceptions.TransactionNotReadableException)

Example 2 with TransactionNotReadableException

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);
    }
}
Also used : Transaction(javax.jdo.Transaction) Random(java.util.Random) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) TransactionNotReadableException(org.datanucleus.api.jdo.exceptions.TransactionNotReadableException) Collection(java.util.Collection) Dog(org.datanucleus.samples.metadata.animal.Dog)

Aggregations

TransactionNotReadableException (org.datanucleus.api.jdo.exceptions.TransactionNotReadableException)2 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 Random (java.util.Random)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 Transaction (org.datanucleus.Transaction)1 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)1 Dog (org.datanucleus.samples.metadata.animal.Dog)1