我有以下代码:
EntityManager mgr = null;
try {
mgr = getEntityManager();
ZooperTheme2 theme = null;
theme = mgr.find(Theme2.class, "1");
theme.setPopularity(7);
mgr.persist(theme);
for (int c = 1; c < 204; c++) {
theme = mgr.find(Theme2.class, "" + c);
theme.calculatePopularity();
mgr.persist(theme);
}
} catch (Exception e) {
} finally {
mgr.close();
}
'For'循环之前的部分:
theme = mgr.find(Theme2.class, "1");
theme.setPopularity(7);
mgr.persist(theme);
单独使用 。但是,当我添加'For'循环来迭代所有主题时,没有任何主题正在更新(包括主题“1”使用.setPopularity(7)。日志文件显示以下错误:
Uncaught exception from servlet
javax.persistence.PersistenceException: Illegal argument
at org.datanucleus.api.jpa.NucleusJPAHelper.getJPAExceptionForNucleusException(NucleusJPAHelper.java:298)
at org.datanucleus.api.jpa.JPAEntityManager.close(JPAEntityManager.java:197)
at com.companionfree.zooperthemeviewer.CronController.doGet(CronController.java:43)
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.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.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 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.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:266)
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 com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:146)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:446)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:437)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:444)
at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:188)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:308)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:300)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:441)
at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:251)
at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.IllegalArgumentException: cross-group transaction need to be explicitly specified, see TransactionOptions.Builder.withXG
at com.google.appengine.api.datastore.DatastoreApiHelper.translateError(DatastoreApiHelper.java:39)
at com.google.appengine.api.datastore.DatastoreApiHelper$1.convertException(DatastoreApiHelper.java:76)
at com.google.appengine.api.utils.FutureWrapper.get(FutureWrapper.java:94)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$6.getFutureWithOptionalTimeout(AsyncDatastoreServiceImpl.java:373)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$6.aggregate(AsyncDatastoreServiceImpl.java:345)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$6.get(AsyncDatastoreServiceImpl.java:316)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$6.get(AsyncDatastoreServiceImpl.java:305)
at com.google.appengine.api.datastore.FutureHelper$TxnAwareFuture.get(FutureHelper.java:171)
at com.google.appengine.api.utils.FutureWrapper.get(FutureWrapper.java:86)
at com.google.appengine.api.utils.FutureWrapper.get(FutureWrapper.java:86)
at com.google.appengine.api.utils.FutureWrapper.get(FutureWrapper.java:86)
at com.google.appengine.api.datastore.FutureHelper.getInternal(FutureHelper.java:71)
at com.google.appengine.api.datastore.FutureHelper.quietGet(FutureHelper.java:58)
at com.google.appengine.api.datastore.DatastoreServiceImpl.get(DatastoreServiceImpl.java:51)
at com.google.appengine.datanucleus.WrappedDatastoreService.get(WrappedDatastoreService.java:70)
at com.google.appengine.datanucleus.EntityUtils.getEntityFromDatastore(EntityUtils.java:667)
at com.google.appengine.datanucleus.DatastorePersistenceHandler.updateObject(DatastorePersistenceHandler.java:415)
at org.datanucleus.state.JDOStateManager.flush(JDOStateManager.java:3841)
at org.datanucleus.ObjectManagerImpl.flushInternalWithOrdering(ObjectManagerImpl.java:3888)
at org.datanucleus.ObjectManagerImpl.flushInternal(ObjectManagerImpl.java:3811)
at org.datanucleus.ObjectManagerImpl.flush(ObjectManagerImpl.java:3751)
at org.datanucleus.ObjectManagerImpl.preCommit(ObjectManagerImpl.java:4141)
at org.datanucleus.ObjectManagerImpl.transactionPreCommit(ObjectManagerImpl.java:428)
at org.datanucleus.TransactionImpl.internalPreCommit(TransactionImpl.java:398)
at org.datanucleus.TransactionImpl.commit(TransactionImpl.java:287)
at org.datanucleus.ObjectManagerImpl.close(ObjectManagerImpl.java:1090)
at org.datanucleus.api.jpa.JPAEntityManager.close(JPAEntityManager.java:193)
... 35 more
CronController.java:43是这样的: mgr.close()
当我添加For循环时,似乎我的代码失败了,但我不知道为什么......
答案 0 :(得分:0)
“应用程序使用工厂实例为访问数据存储区的每个请求创建一个EntityManager实例。”
我了解到,如果您尝试在多个对象上使用它,EntityManager只能调用一次persist并出错。我的新代码是:
EntityManager mgr = null; Theme2 theme = null;
for (int c = 1;c<204;c++) {
try {
mgr = getEntityManager();
theme = mgr.find(Theme2.class, "" + c);
theme.calculatePopularity();
mgr.persist(theme);
} catch (Exception e) {
} finally {
mgr.close();
}
}
它完美无缺:)