Search in sources :

Example 1 with Transaction

use of javax.jdo.Transaction in project OpenRefine by OpenRefine.

the class AppEngineRefineBrokerImpl method expireLocks.

protected void expireLocks(HttpServletResponse response) throws Exception {
    PersistenceManager pm = pmfInstance.getPersistenceManager();
    try {
        Extent<Lock> extent = pm.getExtent(Lock.class, false);
        try {
            for (Lock lock : extent) {
                if (lock.timestamp + LOCK_DURATION < System.currentTimeMillis()) {
                    Transaction tx = pm.currentTransaction();
                    try {
                        tx.begin();
                        pm.deletePersistent(lock);
                        tx.commit();
                    } finally {
                        if (tx.isActive()) {
                            tx.rollback();
                        }
                    }
                }
            }
        } finally {
            extent.closeAll();
        }
        respond(response, OK);
    } finally {
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 2 with Transaction

use of javax.jdo.Transaction in project OpenRefine by OpenRefine.

the class AppEngineRefineBrokerImpl method obtainLock.

protected void obtainLock(HttpServletResponse response, String pid, String uid, int locktype, String lockvalue) throws Exception {
    PersistenceManager pm = pmfInstance.getPersistenceManager();
    try {
        Lock lock = getLock(pm, pid);
        if (lock == null) {
            Transaction tx = pm.currentTransaction();
            try {
                tx.begin();
                lock = new Lock(Long.toHexString(tx.hashCode()), pid, uid);
                pm.makePersistent(lock);
                tx.commit();
            } finally {
                if (tx.isActive()) {
                    tx.rollback();
                }
            }
        }
        respond(response, lockToJSON(lock));
    } finally {
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 3 with Transaction

use of javax.jdo.Transaction in project OpenRefine by OpenRefine.

the class AppEngineRefineBrokerImpl method startProject.

// ----------------------------------------------------------------------------------------------------
protected void startProject(HttpServletResponse response, String pid, String uid, String lid, String data) throws Exception {
    PersistenceManager pm = pmfInstance.getPersistenceManager();
    try {
        checkLock(pm, pid, uid, lid);
        Project project = getProject(pm, pid);
        if (project != null) {
            throw new RuntimeException("Project '" + pid + "' already exists");
        }
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            project = new Project(pid, data);
            pm.makePersistent(project);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        respond(response, OK);
    } finally {
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 4 with Transaction

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

the class GuideToJDO method QueryJPQL.

@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();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) List(java.util.List) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory)

Example 5 with Transaction

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

the class GuideToJDO method UpdateProducts.

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

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