Exception: Query result sets are not modifiable

允我心安 提交于 2019-12-11 05:48:47

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/3913583/exception-query-result-sets-are-not-modifiable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!