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();
}
}
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();
}
}
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();
}
}
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();
}
}
}
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();
}
}
Aggregations