2

例外:查询结果集不可修改 我在这里看到了类似的问题,但它没有回答我的问题,所以我决定开始一个新的。

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

每当用户添加一个新航班时,它不会直接保存新航班对象,而是将其作为静态变量存储在 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)

我下面的代码基于谷歌应用引擎示例 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();
        }
    }

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

4

1 回答 1

4

所以查询结果是不可修改的,所以不要尝试向它们添加对象。我没有看到任何稍微不正确/不一致的地方(实际上是 JDO/JPA 规范所说的)

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

于 2012-10-06T07:59:43.227 回答