javax.jdo.Transaction Java Examples
The following examples show how to use
javax.jdo.Transaction.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GuideToJDO.java From tutorials with MIT License | 6 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) public void listXMLProducts() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumdXML, null); PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); Query q = pm.newQuery("SELECT FROM " + ProductXML.class.getName()); List<ProductXML> products = (List<ProductXML>) q.execute(); Iterator<ProductXML> iter = products.iterator(); while (iter.hasNext()) { ProductXML p = iter.next(); LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.getName(), p.getPrice() }); pm.deletePersistent(p); } LOGGER.log(Level.INFO, "--------------------------------------------------------------"); tx.commit(); } finally { if (tx.isActive()) { tx.rollback(); } pm.close(); } }
Example #2
Source File: GuideToJDO.java From tutorials with MIT License | 6 votes |
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(); } }
Example #3
Source File: GuideToJDO.java From tutorials with MIT License | 6 votes |
@SuppressWarnings("rawtypes") public void UpdateProducts() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); Query query = pm.newQuery(Product.class, "name == \"Phone\""); Collection result = (Collection) query.execute(); Product product = (Product) result.iterator().next(); product.setName("Android Phone"); tx.commit(); } finally { if (tx.isActive()) { tx.rollback(); } pm.close(); } }
Example #4
Source File: GuideToJDO.java From tutorials with MIT License | 6 votes |
@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(); } }
Example #5
Source File: GuideToJDO.java From tutorials with MIT License | 6 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) public void ListProducts() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); Query q = pm.newQuery("SELECT FROM " + Product.class.getName() + " WHERE price > 10"); List<Product> products = (List<Product>) q.execute(); Iterator<Product> iter = products.iterator(); while (iter.hasNext()) { Product p = iter.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(); } }
Example #6
Source File: JdoTransactionManager.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override protected void doRollback(DefaultTransactionStatus status) { JdoTransactionObject txObject = (JdoTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Rolling back JDO transaction on PersistenceManager [" + txObject.getPersistenceManagerHolder().getPersistenceManager() + "]"); } try { Transaction tx = txObject.getPersistenceManagerHolder().getPersistenceManager().currentTransaction(); if (tx.isActive()) { tx.rollback(); } } catch (JDOException ex) { throw new TransactionSystemException("Could not roll back JDO transaction", ex); } }
Example #7
Source File: JdoTransactionManager.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override protected void doCommit(DefaultTransactionStatus status) { JdoTransactionObject txObject = (JdoTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Committing JDO transaction on PersistenceManager [" + txObject.getPersistenceManagerHolder().getPersistenceManager() + "]"); } try { Transaction tx = txObject.getPersistenceManagerHolder().getPersistenceManager().currentTransaction(); tx.commit(); } catch (JDOException ex) { // Assumably failed to flush changes to database. throw convertJdoAccessException(ex); } }
Example #8
Source File: GuideToJDO.java From tutorials with MIT License | 6 votes |
public void persistXML() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumdXML, null); PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); ProductXML productXML = new ProductXML(0, "Tablet", 80.0); pm.makePersistent(productXML); ProductXML productXML2 = new ProductXML(1, "Phone", 20.0); pm.makePersistent(productXML2); ProductXML productXML3 = new ProductXML(2, "Laptop", 200.0); pm.makePersistent(productXML3); tx.commit(); } finally { if (tx.isActive()) { tx.rollback(); } pm.close(); } }
Example #9
Source File: JdoTransactionManager.java From lams with GNU General Public License v2.0 | 6 votes |
@Override protected void doRollback(DefaultTransactionStatus status) { JdoTransactionObject txObject = (JdoTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Rolling back JDO transaction on PersistenceManager [" + txObject.getPersistenceManagerHolder().getPersistenceManager() + "]"); } try { Transaction tx = txObject.getPersistenceManagerHolder().getPersistenceManager().currentTransaction(); if (tx.isActive()) { tx.rollback(); } } catch (JDOException ex) { throw new TransactionSystemException("Could not roll back JDO transaction", ex); } }
Example #10
Source File: JdoTransactionManager.java From lams with GNU General Public License v2.0 | 6 votes |
@Override protected void doCommit(DefaultTransactionStatus status) { JdoTransactionObject txObject = (JdoTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Committing JDO transaction on PersistenceManager [" + txObject.getPersistenceManagerHolder().getPersistenceManager() + "]"); } try { Transaction tx = txObject.getPersistenceManagerHolder().getPersistenceManager().currentTransaction(); tx.commit(); } catch (JDOException ex) { // Assumably failed to flush changes to database. throw convertJdoAccessException(ex); } }
Example #11
Source File: GuideToJDO.java From tutorials with MIT License | 5 votes |
@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(); } }
Example #12
Source File: GuideToJDO.java From tutorials with MIT License | 5 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) public void QuerySQL() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); // SQL : LOGGER.log(Level.INFO, "SQL --------------------------------------------------------------"); Query query = pm.newQuery("javax.jdo.query.SQL", "SELECT * FROM PRODUCT"); query.setClass(Product.class); List<Product> results = query.executeList(); Iterator<Product> iter = results.iterator(); while (iter.hasNext()) { Product p = iter.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(); } }
Example #13
Source File: GuideToJDO.java From tutorials with MIT License | 5 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) public void QueryJPQL() { PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); // JPQL : LOGGER.log(Level.INFO, "JPQL --------------------------------------------------------------"); Query q = pm.newQuery("JPQL", "SELECT p FROM " + Product.class.getName() + " p WHERE p.name = 'Laptop'"); List results = (List) q.execute(); Iterator<Product> iter = results.iterator(); while (iter.hasNext()) { Product p = iter.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(); } }
Example #14
Source File: JDOTest.java From hyperjaxb3 with BSD 2-Clause "Simplified" License | 5 votes |
private void storeEmployee(Employee[] emp) { PersistenceManager pm = getPersistenceManager(); Transaction tx = pm.currentTransaction(); tx.begin(); pm.makePersistent(emp[0]); pm.makePersistent(emp[1]); tx.commit(); pm.close(); Set projects = emp[0].getProjects(); emp[0].getName(); projects.size(); }
Example #15
Source File: TestSentryRole.java From incubator-sentry with Apache License 2.0 | 5 votes |
private void commitTransaction(PersistenceManager pm) { Transaction currentTransaction = pm.currentTransaction(); try { Preconditions.checkState(currentTransaction.isActive(), "Transaction is not active"); currentTransaction.commit(); } finally { pm.close(); } }
Example #16
Source File: SentryStore.java From incubator-sentry with Apache License 2.0 | 5 votes |
public void rollbackTransaction(PersistenceManager pm) { if (pm == null || pm.isClosed()) { return; } Transaction currentTransaction = pm.currentTransaction(); if (currentTransaction.isActive()) { try { currentTransaction.rollback(); } finally { pm.close(); } } }
Example #17
Source File: SentryStore.java From incubator-sentry with Apache License 2.0 | 5 votes |
public void commitTransaction(PersistenceManager pm) { Transaction currentTransaction = pm.currentTransaction(); try { Preconditions.checkState(currentTransaction.isActive(), "Transaction is not active"); currentTransaction.commit(); } finally { pm.close(); } }
Example #18
Source File: DefaultJdoDialect.java From lams with GNU General Public License v2.0 | 5 votes |
/** * This implementation invokes the standard JDO {@link Transaction#begin()} * method and also {@link Transaction#setIsolationLevel(String)} if necessary. * @see javax.jdo.Transaction#begin * @see org.springframework.transaction.InvalidIsolationLevelException */ @Override public Object beginTransaction(Transaction transaction, TransactionDefinition definition) throws JDOException, SQLException, TransactionException { String jdoIsolationLevel = getJdoIsolationLevel(definition); if (jdoIsolationLevel != null) { transaction.setIsolationLevel(jdoIsolationLevel); } transaction.begin(); return null; }
Example #19
Source File: JDOPMRetriever.java From seldon-server with Apache License 2.0 | 5 votes |
public void cleanup() { try { Map<String,PersistenceManager> map = (Map<String,PersistenceManager>)super.get(); if (map != null) for(PersistenceManager pm : map.values()) { if(pm == null) return; try { if(!pm.isClosed()) { TransactionPeer.closeReadOnlyTransaction(pm); Transaction ts = pm.currentTransaction(); if(ts.isActive()) { logger.warn("transaction stil active"); ts.rollback(); } pm.close(); } else { logger.warn("pm is closed"); } } catch(Exception ex) { logger.warn("exception on cleanup",ex); } } } finally { set(null); } }
Example #20
Source File: JdoTransactionManager.java From spring4-understanding with Apache License 2.0 | 5 votes |
public void setRollbackOnly() { Transaction tx = this.persistenceManagerHolder.getPersistenceManager().currentTransaction(); if (tx.isActive()) { tx.setRollbackOnly(); } if (hasConnectionHolder()) { getConnectionHolder().setRollbackOnly(); } }
Example #21
Source File: GuideToJDOIntegrationTest.java From tutorials with MIT License | 5 votes |
@Test public void givenProduct_WhenNewThenPerformTransaction() { PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null); pumd.addClassName("com.baeldung.libraries.jdo.Product"); pumd.setExcludeUnlistedClasses(); pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver"); pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence"); pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa"); pumd.addProperty("javax.jdo.option.ConnectionPassword", ""); pumd.addProperty("datanucleus.autoCreateSchema", "true"); pumd.addProperty("datanucleus.schema.autoCreateTables", "true"); PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); for (int i = 0; i < 100; i++) { String nam = "Product-" + i; Product productx = new Product(nam, (double) i); pm.makePersistent(productx); } tx.commit(); } catch (Throwable thr) { fail("Failed test : " + thr.getMessage()); } finally { if (tx.isActive()) { tx.rollback(); } pm.close(); } pmf.close(); }
Example #22
Source File: DefaultJdoDialect.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * This implementation invokes the standard JDO {@link Transaction#begin()} * method and also {@link Transaction#setIsolationLevel(String)} if necessary. * @see javax.jdo.Transaction#begin * @see org.springframework.transaction.InvalidIsolationLevelException */ @Override public Object beginTransaction(Transaction transaction, TransactionDefinition definition) throws JDOException, SQLException, TransactionException { String jdoIsolationLevel = getJdoIsolationLevel(definition); if (jdoIsolationLevel != null) { transaction.setIsolationLevel(jdoIsolationLevel); } transaction.begin(); return null; }
Example #23
Source File: JdoTransactionManager.java From lams with GNU General Public License v2.0 | 5 votes |
public void setRollbackOnly() { Transaction tx = this.persistenceManagerHolder.getPersistenceManager().currentTransaction(); if (tx.isActive()) { tx.setRollbackOnly(); } if (hasConnectionHolder()) { getConnectionHolder().setRollbackOnly(); } }
Example #24
Source File: TestSentryRole.java From incubator-sentry with Apache License 2.0 | 4 votes |
private PersistenceManager openTransaction() { PersistenceManager pm = pmf.getPersistenceManager(); Transaction currentTransaction = pm.currentTransaction(); currentTransaction.begin(); return pm; }
Example #25
Source File: RoundtripTest.java From hyperjaxb3 with BSD 2-Clause "Simplified" License | 4 votes |
@Override protected void checkSample(File sample) throws Exception { // TODO Auto-generated method stub final JAXBContext context = createContext(); logger.debug("Unmarshalling."); final Unmarshaller unmarshaller = context.createUnmarshaller(); // Unmarshall the document final JAXBElement element = (JAXBElement) unmarshaller .unmarshal(sample); final Object object = element.getValue(); logger.debug("Opening session."); // Open the session, save object into the database logger.debug("Saving the object."); final PersistenceManager saveManager = createPersistenceManager(); // saveManager.setDetachAllOnCommit(true); final Transaction saveTransaction = saveManager.currentTransaction(); saveTransaction.setNontransactionalRead(true); saveTransaction.begin(); // final Object merged = saveSession.merge(object); // saveSession.replicate(object, ReplicationMode.OVERWRITE); // saveSession.get // final Serializable id = final Object mergedObject = saveManager.makePersistent(object); // final Object asd = saveManager.detachCopy(object); saveTransaction.commit(); // final Object id = saveManager.getObjectId(mergedObject); final Object identity = JDOHelper.getObjectId(object); final Object id = identity instanceof SingleFieldIdentity ? ((SingleFieldIdentity) identity).getKeyAsObject() : identity; // Close the session saveManager.close(); logger.debug("Opening session."); // Open the session, load the object final PersistenceManager loadManager = createPersistenceManager(); final Transaction loadTransaction = loadManager.currentTransaction(); loadTransaction.setNontransactionalRead(true); logger.debug("Loading the object."); final Object loadedObject = loadManager.getObjectById(mergedObject.getClass(), id); logger.debug("Closing the session."); final JAXBElement mergedElement = new JAXBElement(element.getName(), element.getDeclaredType(), object); final JAXBElement loadedElement = new JAXBElement(element.getName(), element.getDeclaredType(), loadedObject); logger.debug("Checking the document identity."); logger.debug("Source object:\n" + ContextUtils.toString(context, mergedElement)); logger.debug("Result object:\n" + ContextUtils.toString(context, loadedElement)); checkObjects(mergedObject, loadedObject); loadManager.close(); }
Example #26
Source File: JdoTransactionManagerTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Before public void setUp() { pmf = mock(PersistenceManagerFactory.class); pm = mock(PersistenceManager.class); tx = mock(Transaction.class); }
Example #27
Source File: JdoTransactionManager.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override public boolean isRollbackOnly() { Transaction tx = this.persistenceManagerHolder.getPersistenceManager().currentTransaction(); return tx.getRollbackOnly(); }
Example #28
Source File: JdoTransactionManager.java From lams with GNU General Public License v2.0 | 4 votes |
@Override public boolean isRollbackOnly() { Transaction tx = this.persistenceManagerHolder.getPersistenceManager().currentTransaction(); return tx.getRollbackOnly(); }
Example #29
Source File: GuideToJDOIntegrationTest.java From tutorials with MIT License | 4 votes |
@Test public void givenProduct_WhenQueryThenExist() { PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null); pumd.addClassName("com.baeldung.libraries.jdo.Product"); pumd.setExcludeUnlistedClasses(); pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver"); pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence"); pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa"); pumd.addProperty("javax.jdo.option.ConnectionPassword", ""); pumd.addProperty("datanucleus.autoCreateSchema", "true"); pumd.addProperty("datanucleus.schema.autoCreateTables", "true"); 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); tx.commit(); } catch (Throwable thr) { fail("Failed test : " + thr.getMessage()); } finally { if (tx.isActive()) { tx.rollback(); } pm.close(); } pmf.close(); PersistenceManagerFactory pmf2 = new JDOPersistenceManagerFactory(pumd, null); PersistenceManager pm2 = pmf2.getPersistenceManager(); Transaction tx2 = pm2.currentTransaction(); try { tx2.begin(); @SuppressWarnings("rawtypes") Query q = pm2.newQuery("SELECT FROM " + Product.class.getName() + " WHERE price == 200"); @SuppressWarnings("unchecked") List<Product> products = (List<Product>) q.execute(); for (Product p : products) { assertEquals("Laptop", p.name); } tx2.commit(); } finally { if (tx2.isActive()) { tx2.rollback(); } pm2.close(); } }
Example #30
Source File: SentryStore.java From incubator-sentry with Apache License 2.0 | 3 votes |
/** * PersistenceManager object and Transaction object have a one to one * correspondence. Each PersistenceManager object is associated with a * transaction object and vice versa. Hence we create a persistence manager * instance when we create a new transaction. We create a new transaction * for every store API since we want that unit of work to behave as a * transaction. * * Note that there's only one instance of PersistenceManagerFactory object * for the service. * * Synchronized because we obtain persistence manager */ public synchronized PersistenceManager openTransaction() { PersistenceManager pm = pmf.getPersistenceManager(); Transaction currentTransaction = pm.currentTransaction(); currentTransaction.begin(); return pm; }