Google App Engine JDO - 查询结果集不可修改

时间:2012-10-06 07:13:55

标签: google-app-engine jdo

Exception: Query result sets are not modifiable 我在这里看到了类似的问题,但它没有回答我的问题所以我决定开始一个新问题。

我正在尝试通过将最后添加的对象临时存储在静态变量中来解决持续不一致的读取问题(https://groups.google.com/forum/?fromgroups=#!topic/google-appengine/E95wH0RkJUE)与Google App Engine的High Replication数据存储区。

每当用户添加新航班时,它不是直接持久保存新航班对象,而是将其作为静态变量存储在PMF中。

因此,当在查询中获取航班列表时,它会将结果存储在列表中,并将列表与最后添加的对象进行比较,如果最后添加的对象不在列表中,我手动添加最后一个将对象添加到列表中。

但是,使用此方法时,只要列表中缺少新添加的对象并手动添加它,我就会收到以下异常。

Oct 6, 2012 2:19:38 PM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: /
java.lang.UnsupportedOperationException: Query result sets are not modifiable
    at org.datanucleus.store.query.AbstractQueryResult.add(AbstractQueryResult.java:222)
    at com.jimfoo88.helloorm.GetFlights.queryJDO(GetFlights.java:101)
    at com.jimfoo88.helloorm.GetFlights.doGet(GetFlights.java:50)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:35)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:60)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:94)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:370)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

下面的代码基于Google应用引擎示例HelloORM http://code.google.com/p/datanucleus-appengine/source/browse/trunk/demos/helloorm/src/com/google/appengine/demos/helloorm/GetFlights.java?r=736

我只显示我修改的部分以简化阅读。

AddFlight.java

private void doPostJDO(Flight f) {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    PMF.setLastObjectAdded(f);
    try {
      pm.makePersistent(f);
    } finally {
      pm.close();
    }
  }

PMF.java

// Copyright 2008 Google Inc. All Rights Reserved.
package com.jimfoo88.helloorm;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

/**
 * @author Max Ross <maxr@google.com>
 */
public final class PMF {

    private static Object lastObjectAdded;

  private static final PersistenceManagerFactory INSTANCE = JDOHelper.getPersistenceManagerFactory("transactions-optional");

  public static Object getLastObjectAdded() {
    return lastObjectAdded;
}

public static void setLastObjectAdded(Object lastObject) {
    lastObjectAdded = lastObject;
}

public static PersistenceManagerFactory get() {
    return INSTANCE;
  }

  private PMF() {}
}

GetFlights.java

private Collection<Flight> queryJDO(String query) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            List<Flight> flights = (List<Flight>) pm.newQuery(query).execute();
            // Force all results to be pulled back before we close the entity
            // manager.
            // We could have also called pm.detachCopyAll()
            flights.size();
            //pm.detachCopyAll(flights);
            boolean found = false;
            try {
                for (Flight f : flights) {
                    if ((Flight) PMF.getLastObjectAdded() == null) {
                        found = true;
                        break;
                    }
                    if (f.getDest() == ((Flight) PMF.getLastObjectAdded())
                            .getDest()
                            && f.getOrig() == ((Flight) PMF
                                    .getLastObjectAdded()).getOrig()) {
                        logger.info("found object " + f.toString()
                                + "in datastore");
                        found = true;
                        break;
                    }
                }
                if (found == false) {
                    logger.info("did not find object "
                            + (Flight) PMF.getLastObjectAdded()
                            + " in datastore, need to add it");
                    flights.add((Flight) PMF.getLastObjectAdded());
                }
            } catch (NullPointerException e) {
                logger.error(e.getMessage());
            }
            return flights;
        } finally {
            pm.close();
        }
    }

如果有人能指出我的错误,我们将不胜感激。如果有更好的方法可以解决这个问题,请告诉我。感谢。

1 个答案:

答案 0 :(得分:3)

因此查询结果不可修改,因此请勿尝试向其添加对象。我没有看到任何有关它的错误/不一致(实际上是JDO / JPA规范所说的)

如果您想在以后的上下文中使用这些结果,您可以轻松创建新的List,使用查询结果执行addAll(),并使用该List