Search in sources :

Example 1 with TransactionNotActiveException

use of org.datanucleus.api.jdo.exceptions.TransactionNotActiveException in project tests by datanucleus.

the class PersistenceManagerTest method testGetObjectByIdNonTransactional.

/**
 * Test for getObjectById() when the object is retrieved from the L2 cache.
 * @throws Exception
 */
public void testGetObjectByIdNonTransactional() throws Exception {
    try {
        Object id = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Insert a record for use later
            BigDecimal bd = new BigDecimal("12345.12345");
            BigInteger bi = new BigInteger("12345");
            java.util.Date date1 = (new java.util.GregorianCalendar()).getTime();
            java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01");
            java.sql.Time time3 = java.sql.Time.valueOf("10:01:59");
            java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2001-01-01 23:23:23.050500000");
            tx.begin();
            Primitive p = new Primitive();
            setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", bd, bi, date1, date2, time3, timestamp);
            pm.makePersistent(p);
            tx.commit();
            id = pm.getObjectId(p);
            tx.begin();
            Primitive p1 = (Primitive) pm.getObjectById(id);
            Object id1 = pm.getObjectId(p1);
            assertEquals(id, id1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object in a nontransactionalRead PMF from L2 cache
        PersistenceManager pm2 = null;
        try {
            pm = pmf.getPersistenceManager();
            try {
                // first getObjectById() will result in hollow instance that
                // is put in L2 cache
                Object pc = pm.getObjectById(id);
                // pin pc in the L2 cache to make sure it is not garbage collected
                pmf.getDataStoreCache().pin(pc);
                // take a new PM to have an empty L1 cache
                pm2 = pmf.getPersistenceManager();
                // take PC from L2 cache
                pm2.getObjectById(id);
            } catch (TransactionNotActiveException e) {
                LOG.error("Exception thrown in test", e);
                fail("TransactionNotActiveException thrown even though nontransactionRead is true");
            }
        } finally {
            if (!pm.isClosed()) {
                pm.close();
            }
            if (pm2 != null) {
                pm2.close();
            }
        }
    } finally {
        // Clean out our data
        clean(Primitive.class);
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) BigDecimal(java.math.BigDecimal) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) Transaction(javax.jdo.Transaction) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) BigInteger(java.math.BigInteger)

Example 2 with TransactionNotActiveException

use of org.datanucleus.api.jdo.exceptions.TransactionNotActiveException 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

PersistenceManager (javax.jdo.PersistenceManager)2 Transaction (javax.jdo.Transaction)2 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)2 TransactionNotActiveException (org.datanucleus.api.jdo.exceptions.TransactionNotActiveException)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 Random (java.util.Random)1 TransactionNotWritableException (org.datanucleus.api.jdo.exceptions.TransactionNotWritableException)1 Dog (org.datanucleus.samples.metadata.animal.Dog)1 InversePrimitive (org.datanucleus.samples.widget.InversePrimitive)1 Primitive (org.datanucleus.samples.widget.Primitive)1