Exception: Query result sets are not modifiable

2019-07-31 14:09发布

I'm trying to write to a JDO store using this code:

    PersistenceManager pm = PMF.get().getPersistenceManager();

    try {
        pm.currentTransaction().begin();

        // deactivate all for current domain
        Query q = pm.newQuery(CampaignStore.class, "domain == '" + domain +"'");
        Collection result = (Collection) q.execute();

        CampaignStore toBeEdited = null;
        Iterator iter = result.iterator();
        while (iter.hasNext()) {
            toBeEdited = (CampaignStore) iter.next();
            toBeEdited.setActive(false);
        }
        result.clear();

        // set new one active
        q = pm.newQuery(CampaignStore.class, "id == " + id);
        result = (Collection) q.execute();
        toBeEdited = (CampaignStore) result.iterator().next();
        if (toBeEdited == null) {
            LOG.log(Level.WARNING, "setActiveCampaign: Unable to find Campaign ID '"+ id +"'");
            pm.currentTransaction().rollback();
            return;
        }           
        toBeEdited.setActive(true);

        pm.currentTransaction().commit();
        LOG.log(Level.INFO, "setActiveCampaign: Active Campaign ID is now '"+ id +"'");
    }
    catch (Exception e) {
        pm.currentTransaction().rollback();
        LOG.log(Level.WARNING, "setActiveCampaign: Exception: "+ e.getMessage());
    } finally {
        pm.close();
    }

Unfortunately I get an "Query result sets are not modifiable" exception.

I'm quite sure it comes from the first query with the iteration, cause the second one alone will work.

Any ideas what I need to change to make the query result modifiable?

2条回答
狗以群分
2楼-- · 2019-07-31 14:28

What is result.clear() ? You can't clear results like that. q.closeAll() makes more sense.

查看更多
等我变得足够好
3楼-- · 2019-07-31 14:37

I removed the try/catch block and got a more detailed message "java.lang.IllegalArgumentException: can't operate on multiple entity groups in a single transaction.", which helped me solve my problem.

I needed to turn off transactions:

pmfInstance.setNontransactionalRead(true);
pmfInstance.setNontransactionalWrite(true);

I don't need the code to be transaction safe.

Here you can find more information: http://groups.google.com/group/google-appengine-java/browse_thread/thread/04f35b443c15d531

查看更多
登录 后发表回答