Search in sources :

Example 11 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class StorageTester method validateTransactionalRefresh.

protected void validateTransactionalRefresh(Class c) throws Exception {
    // Validate that persistent non-transactional objects transition to persistent,
    // and refresh themselves, when accessed from within a transaction
    TestHelper.LOG.info("Validating transactional refresh on " + TEST_OBJECT_COUNT + " " + c.getName() + " objects");
    PersistenceManager pm1 = pmf.getPersistenceManager();
    Transaction tx1 = pm1.currentTransaction();
    tx1.setRetainValues(true);
    try {
        PersistenceManager pm2 = pmf.getPersistenceManager();
        Transaction tx2 = pm2.currentTransaction();
        Random rnd = new Random(0);
        TestObject[] pobjs = new TestObject[TEST_OBJECT_COUNT];
        try {
            // Load all of the objects using pm1
            tx1.begin();
            for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
                // Half will be Hollow and half PersistentClean
                boolean validate = rnd.nextBoolean();
                pobjs[i] = (TestObject) pm1.getObjectById(ids[i], validate);
                // Half of the PersistentClean will be fully loaded
                if (validate && rnd.nextBoolean())
                    assertFieldsEqual(objs[i], pobjs[i]);
            }
            tx1.commit();
            for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
                Assert.assertTrue("Object is not persistent: " + ids[i], JDOHelper.isPersistent(pobjs[i]));
                Assert.assertTrue("Object is transactional: " + ids[i], !JDOHelper.isTransactional(pobjs[i]));
            }
            // Modify them all using pm2
            tx2.begin();
            for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
                TestObject obj = (TestObject) pm2.getObjectById(ids[i], false);
                obj.fillUpdateRandom();
                objs[i] = (TestObject) obj.clone();
                assertFieldsEqual(obj, objs[i]);
            }
            tx2.commit();
            if (!tx1.getOptimistic()) {
                // Access them all inside a transaction using pm1
                tx1.begin();
                for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
                    Assert.assertTrue("Object is not persistent: " + ids[i], JDOHelper.isPersistent(pobjs[i]));
                    Assert.assertTrue("Object is transactional: " + ids[i], !JDOHelper.isTransactional(pobjs[i]));
                    assertFieldsEqual(objs[i], pobjs[i]);
                    Assert.assertTrue("Object is not persistent: " + ids[i], JDOHelper.isPersistent(pobjs[i]));
                    Assert.assertTrue("Object is not transactional: " + ids[i], JDOHelper.isTransactional(pobjs[i]));
                }
                tx1.commit();
            }
        } finally {
            if (tx2.isActive())
                tx2.rollback();
            pm2.close();
        }
    } catch (Exception e) {
        TestHelper.LOG.error("StorageTester.validateTransactionalRefresh exception thrown", e);
        throw e;
    } finally {
        if (tx1.isActive())
            tx1.rollback();
        pm1.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Random(java.util.Random) PersistenceManager(javax.jdo.PersistenceManager)

Example 12 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class StorageTester method insertObjects.

/**
 * Method to insert a series of test objects of the specified type
 * @param c The class whose objects we create
 * @throws Exception
 */
public void insertObjects(Class c) throws Exception {
    // Insert TEST_OBJECT_COUNT random objects
    TestHelper.LOG.info("Inserting " + TEST_OBJECT_COUNT + " " + c.getName() + " objects");
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
            tx.setRetainValues(true);
            tx.begin();
            TestObject obj = (TestObject) c.newInstance();
            obj.fillRandom();
            objs[i] = (TestObject) obj.clone();
            assertFieldsEqual(obj, objs[i]);
            pm.makePersistent(obj);
            ids[i] = JDOHelper.getObjectId(obj);
            tx.commit();
        }
    } catch (Exception e) {
        TestHelper.LOG.error("StorageTester.insertObjects exception thrown", e);
        throw e;
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 13 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class JDOPersistenceTestCase method closeManagedPms.

private void closeManagedPms() {
    for (PersistenceManager pm : managedPms) {
        if (!pm.isClosed()) {
            Transaction tx = pm.currentTransaction();
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 14 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class ReadUncommittedIsolationLevelTest method testReadUncommited.

public void testReadUncommited() {
    if (!pmf.supportedOptions().contains("javax.jdo.option.TransactionIsolationLevel.read-uncommitted")) {
        // Datastore doesn't support this isolation level
        return;
    } else if (vendorID != null && vendorID.equalsIgnoreCase("hsql")) {
        // HSQL 2.x can lock up on this
        return;
    }
    // Add some initial data
    pm = pmf.getPersistenceManager();
    pm.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "none");
    Transaction tx = pm.currentTransaction();
    Object oid;
    try {
        tx.begin();
        // create sample data
        Office o = new Office(1L, ROOM, "desc");
        o = pm.makePersistent(o);
        oid = pm.getObjectId(o);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    String finalDescription = null;
    PersistenceManager pm1 = pmf.getPersistenceManager();
    PersistenceManager pm2 = pmf.getPersistenceManager();
    Transaction tx1 = pm1.currentTransaction();
    Transaction tx2 = pm2.currentTransaction();
    try {
        pm1.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "none");
        tx1.setIsolationLevel(Constants.TX_READ_UNCOMMITTED);
        tx1.begin();
        pm2.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "none");
        tx2.setIsolationLevel(Constants.TX_READ_UNCOMMITTED);
        tx2.begin();
        Office o1 = (Office) pm1.getObjectById(oid);
        LOG.info("within tx1 after modifying:" + o1.asString());
        finalDescription = o1.getDescription() + o1.getRoomName();
        o1.setDescription(finalDescription);
        // send UPDATE to database
        pm1.flush();
        LOG.info("within tx1 after modifying:" + o1.asString());
        Office o2 = (Office) pm2.getObjectById(oid);
        LOG.info("within tx2: " + o2.asString());
        assertEquals("uncommited modification not seen", finalDescription, o2.getDescription());
    } catch (JDODataStoreException e) {
        assertFalse("Should be able to see description " + finalDescription + " but " + e.getMessage(), true);
    } finally {
        tx2.commit();
        pm2.close();
        tx1.commit();
        pm1.close();
        clean(Office.class);
        clean(Department.class);
    }
}
Also used : Office(org.jpox.samples.models.company.Office) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 15 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class SQLFunctionPersistenceTest method testUpdateWithSQLFunction.

public void testUpdateWithSQLFunction() {
    try {
        SQLFunction function;
        Object id = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            function = new SQLFunction();
            function.setText("upper");
            function.setText1("t1");
            function.setText2("t2");
            function.setText3("t3");
            pm.makePersistent(function);
            id = JDOHelper.getObjectId(function);
            tx.commit();
            tx.begin();
            function.setText2("t2-1");
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Avoid L2 cache interference
        pmf.getDataStoreCache().evictAll(false, SQLFunction.class);
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            function = (SQLFunction) pm.getObjectById(id, true);
            assertEquals("text String retrieved is wrong", "UPPER", function.getText());
            assertEquals("text String retrieved is wrong", "T1", function.getText1());
            assertEquals("text String retrieved is wrong", "valueu", function.getText2());
            assertEquals("text String retrieved is wrong", "T3", function.getText3());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(SQLFunction.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) SQLFunction(org.jpox.samples.rdbms.sqlfunction.SQLFunction)

Aggregations

Transaction (javax.jdo.Transaction)1552 PersistenceManager (javax.jdo.PersistenceManager)1542 Query (javax.jdo.Query)635 List (java.util.List)395 JDOUserException (javax.jdo.JDOUserException)347 Collection (java.util.Collection)287 Iterator (java.util.Iterator)216 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)183 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)156 Person (org.jpox.samples.models.company.Person)144 Employee (org.jpox.samples.models.company.Employee)122 Manager (org.jpox.samples.models.company.Manager)98 HashSet (java.util.HashSet)97 SQLException (java.sql.SQLException)80 ArrayList (java.util.ArrayList)77 JDOException (javax.jdo.JDOException)73 Extent (javax.jdo.Extent)70 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)68 JDODataStoreException (javax.jdo.JDODataStoreException)65 JDOFatalUserException (javax.jdo.JDOFatalUserException)65