Search in sources :

Example 6 with Transaction

use of javax.jdo.Transaction in project tutorials by eugenp.

the class GuideToJDO method DeleteProducts.

@SuppressWarnings("rawtypes")
public void DeleteProducts() {
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query query = pm.newQuery(Product.class, "name == \"Android Phone\"");
        Collection result = (Collection) query.execute();
        Product product = (Product) result.iterator().next();
        pm.deletePersistent(product);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory)

Example 7 with Transaction

use of javax.jdo.Transaction in project tutorials by eugenp.

the class GuideToJDO method CreateProducts.

public void CreateProducts() {
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Product product = new Product("Tablet", 80.0);
        pm.makePersistent(product);
        Product product2 = new Product("Phone", 20.0);
        pm.makePersistent(product2);
        Product product3 = new Product("Laptop", 200.0);
        pm.makePersistent(product3);
        for (int i = 0; i < 100; i++) {
            String nam = "Product-" + i;
            double price = rnd.nextDouble();
            Product productx = new Product(nam, price);
            pm.makePersistent(productx);
        }
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory)

Example 8 with Transaction

use of javax.jdo.Transaction in project tutorials by eugenp.

the class GuideToJDO method QueryJDOQL.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void QueryJDOQL() {
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Declarative JDOQL :
        LOGGER.log(Level.INFO, "Declarative JDOQL --------------------------------------------------------------");
        Query qDJDOQL = pm.newQuery(Product.class);
        qDJDOQL.setFilter("name == 'Tablet' && price == price_value");
        qDJDOQL.declareParameters("double price_value");
        List<Product> resultsqDJDOQL = qDJDOQL.setParameters(80.0).executeList();
        Iterator<Product> iterDJDOQL = resultsqDJDOQL.iterator();
        while (iterDJDOQL.hasNext()) {
            Product p = iterDJDOQL.next();
            LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price });
        }
        LOGGER.log(Level.INFO, "--------------------------------------------------------------");
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory)

Example 9 with Transaction

use of javax.jdo.Transaction in project tutorials by eugenp.

the class MyApp method persistObject.

public static void persistObject(Object obj) {
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(obj);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
    }
}
Also used : Transaction(javax.jdo.Transaction)

Example 10 with Transaction

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

the class StorageTester method validateObjects.

protected void validateObjects(Class c) throws Exception {
    // Read them back and verify that they contain what they should
    TestHelper.LOG.info("Validating " + TEST_OBJECT_COUNT + " " + c.getName() + " objects:");
    TestHelper.LOG.info("  Normal read");
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        TestObject[] loaded = new TestObject[TEST_OBJECT_COUNT];
        for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
            tx.begin();
            TestObject obj = (TestObject) pm.getObjectById(ids[i], true);
            assertFieldsEqual(objs[i], obj);
            loaded[i] = obj;
            tx.commit();
        }
    } catch (Exception e) {
        TestHelper.LOG.error("StorageTester.validateObjects exception thrown", e);
        throw e;
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
    /*
         * Read some of them back and verify them using non-transactional reads.
         * Only some are done because non-transactional reads are much slower
         * unless connection pooling is used (eventually we should use pooling
         * when testing).
         */
    TestHelper.LOG.info("  Non-transactional read");
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.setNontransactionalRead(true);
        for (int i = 0; i < TEST_OBJECT_COUNT; i += 10) {
            TestObject obj = (TestObject) pm.getObjectById(ids[i], false);
            assertFieldsEqual(objs[i], obj);
        }
    } catch (Exception e) {
        TestHelper.LOG.error("StorageTester.validateObjects exception thrown", e);
        throw e;
    } finally {
        pm.close();
    }
    // Read some of them back, verify them, then verify values get retained after commit when retainValues mode is on
    TestHelper.LOG.info("  Retain values mode");
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.setRetainValues(true);
        tx.begin();
        TestObject[] loaded = new TestObject[TEST_OBJECT_COUNT];
        for (int i = 0; i < TEST_OBJECT_COUNT; i += 10) {
            TestObject obj = (TestObject) pm.getObjectById(ids[i], true);
            assertFieldsEqual(objs[i], obj);
            loaded[i] = obj;
        }
        tx.commit();
        for (int i = 0; i < TEST_OBJECT_COUNT; i += 10) assertFieldsEqual(objs[i], loaded[i]);
    } catch (Exception e) {
        TestHelper.LOG.error("StorageTester.validateObjects exception thrown", e);
        throw e;
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

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