Search in sources :

Example 1 with TransactionNotWritableException

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

Example 2 with TransactionNotWritableException

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

Aggregations

TransactionNotWritableException (org.datanucleus.api.jdo.exceptions.TransactionNotWritableException)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 TransactionNotActiveException (org.datanucleus.api.jdo.exceptions.TransactionNotActiveException)1 Dog (org.datanucleus.samples.metadata.animal.Dog)1